0

How do I access dictionaries inside a list?

c = [{'team':2000,'name':'a'}, {'team':3000,'name':'b'}]
values = []
for key, value in c.iteritems():
 values.append((key, value))
 print values

I tried accessing the list using iteritems but I got an error specifying list does not have iteritems. Then, I tried using a for loop but I still get an error specifying list indices must be integers.

Moon Cheesez
2,7213 gold badges29 silver badges41 bronze badges
asked Jun 4, 2016 at 3:04
4
  • @MoonCheesez he's using python 2.x, obviously Commented Jun 4, 2016 at 3:08
  • @DaniilRyzhkov my bad, didn't notice the print statement and was too fast to judge. @Bala could you include the code you used which gave you the error "list indices must be integers"? Commented Jun 4, 2016 at 3:22
  • What is your desired output on your problem? Commented Jun 4, 2016 at 3:42
  • Hey thanks I got it. Commented Jun 20, 2016 at 13:04

3 Answers 3

2

I'm not sure what your desired output is, but the problem is that c is a list() of dict()s, not a dict(). So if you want to loop over every key and value in each dict in the list, you need two loops:

In [85]: for dictionary in c:
 ....: for key, value in dictionary.iteritems():
 ....: values.append((key, value))
 ....: print values
 ....:
[('name', 'a')]
[('name', 'a'), ('team', 2000)]
[('name', 'a'), ('team', 2000), ('name', 'b')]
[('name', 'a'), ('team', 2000), ('name', 'b'), ('team', 3000)]

You can also simplify this with a list comprehension:

In [96]: for dictionary in c:
 values.extend((key, value) for key, value in dictionary.iteritems())
 ....:
In [97]: values
Out[97]: [('name', 'a'), ('team', 2000), ('name', 'b'), ('team', 3000)]

Or, simplify it even further, as list.extend() can take any iterable:

In [112]: for dictionary in c:
 values.extend(dictionary.iteritems())
 .....:
In [113]: values
Out[113]: [('name', 'a'), ('team', 2000), ('name', 'b'), ('team', 3000)]

And, lastly, we can populate values with a single line of code using itertools.chain(), which basically "flattens" multiple iterables into a single iterable (thanks @lvc!):

In [114]: from itertools import chain
In [115]: values = list(chain.from_iterable(d.iteritems() for d in c))
answered Jun 4, 2016 at 3:10
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Love your name, btw, that's a nickname of mine ;)
You can simplify it even further by using the tuples from iteritems directly, rather than unpacking and repacking them - values.extend(dictionary.iteritems()). With itertools, you can then: values = list(chain.from_iterable(d.iteritems() for d in c)).
values.extend(dictionary.iteritems()) does same effect
1

Try this:

c = [{'team':2000,'name':'a'}, {'team':3000,'name':'b'}]
values = []
for row in c: 
 for key, value in row.iteritems():
 if key == 'name': 
 values.append((key, value))
print(values)
# gives this
# [('name', 'a'), ('name', 'b'),]
answered Jun 4, 2016 at 7:11

2 Comments

Hi will when i run this code getting syntax error in print values.can you help me to get rid of this error.And also I want my output should print only name. eg. name:a name:b
@Bala Did this solve it? If so, mark answer as correct
0

"c" is not a dictionary therefore iteritems() will not work. Iterate through the list first to isolate the dictionaries.

>>> c = [{'team':2000,'name':'a'}, {'team':3000,'name':'b'}]
... for i in c:
... for k,v in i.items():
... print "key : "+str(k)+' *** '+ "value : "+str(v)
... key : name *** value : a
... key : team *** value : 2000
... key : name *** value : b
... key : team *** value : 3000
answered Jun 4, 2016 at 6:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.