0

Here's the json

{u'skeleton_horde': 2, u'baby_dragon': 3, u'valkyrie': 5, u'chr_witch': 1, u'lightning': 1, u'order_volley': 6, u'building_inferno': 3, u'battle_ram': 2}

I'm trying to make the list look like this

skeleton_horde baby_dragon valkyrie lightning order_volley building_inferno

Here's the python

print(x['right']['troops'])

There's surprisingly no documentation on how to get the n element of an object (not array). I tried:

print(x['right']['troops'][1])

but it doesn't work.

Mohd
  • 5,523
  • 7
  • 19
  • 30
Tyler L
  • 835
  • 2
  • 16
  • 28

3 Answers3

2

You want to use dict.keys() to a get a list* of the key values of the dict:

print(list(x['right']['troops'].keys()))

*It's actually a view, in Python 3. It would be a list in Python 2.

Scott Colby
  • 1,370
  • 12
  • 25
  • Thank you! I'm used to javascript and iterating across the array :) – Tyler L Jul 30 '17 at 19:43
  • 1
    View or list depends on the Python version. Given the `u` tags on the Unicode strings, I'm guessing Python 2. – user2357112 Jul 30 '17 at 19:44
  • 1
    No need to call `keys()`, you can simply apply `list` to a `dict`: `list({'a':1})` results in `['a']`. – Dan D. Jul 30 '17 at 19:50
  • 1
    @DanD. is absolutely correct. I tend to go for `keys()` for parallelism to `values()` and `items()` and clarity (to myself, at least). If you need every ounce of speed out of your interpreter, avoiding the extra lookup and call might help, (but I bet the list constructor does it anyway...). – Scott Colby Jul 30 '17 at 19:54
2

There's no way of getting the nth item in a dictionary (perhaps you've conflated Python dicts with JavaScript objects) for the simple reason that they are unordered.

There is however a type of dictionary that does maintain the order of its keys, aptly named OrderedDict.

Solution

As another commenter pointed out, there is a solution to your problem, but it still won't give you the keys in the order of definition:

' '.join(obj['right']['troops'])

Note

In a recent version of CPython (3.6), dictionary keys are indeed ordered. I'm not sure if I'd rely on implementation-specific behaviour, or whether you even need to order the keys in this case, but it's good to know. Props to @ScottColby for pointing this out to me!

SwiftsNamesake
  • 1,540
  • 2
  • 11
  • 25
  • 1
    In Python 3.6, [dictionaries are now ordered](https://stackoverflow.com/questions/39980323/dictionaries-are-ordered-in-python-3-6) and this may even be guaranteed in python 3.7. Just another reason to drop Python 2 :) – Scott Colby Jul 30 '17 at 19:49
  • 1
    \*small error in my comment: In **C**Python 3.6. It's not yet guaranteed in other implementations. – Scott Colby Jul 30 '17 at 19:55
  • I'm aware. I checked out the link. I assume you noticed the edit? – SwiftsNamesake Jul 30 '17 at 19:56
  • yes, I do see that now. I just wanted to correct my comment as well--it was your edit that reminded me :) – Scott Colby Jul 30 '17 at 19:57
2

First you want to extract the keys:

x['right']['troops']

Then you want to join them with spaces interspersed

' '.join(x['right']['troops'])

This will be in a different order than what you have, though, since Python dictionaries are unordered.

Andrew Lei
  • 335
  • 2
  • 9