user2487686
user2487686

Reputation: 243

How to access the values in a list of dictionaries?

Say I have a list of dictionaries that have Names and Ages and other info, like so:

thisismylist= [  
              {'Name': 'Albert' , 'Age': 16},
              {'Name': 'Suzy', 'Age': 17},
              {'Name': 'Johnny', 'Age': 13}
]

How would I go about printing the following using a for loop:

Albert
Suzy
Johnny

I just can't wrap my head around this idea...

Upvotes: 21

Views: 122490

Answers (5)

Phantom Coder
Phantom Coder

Reputation: 81

The following code should work perfectly.

for var in thisismylist:
  print var['Name']

Upvotes: 5

dawg
dawg

Reputation: 104102

If you want a list of those values:

>>> [d['Name'] for d in thisismylist]
['Albert', 'Suzy', 'Johnny']

Same method, you can get a tuple of the data:

>>> [(d['Name'],d['Age']) for d in thisismylist]
[('Albert', 16), ('Suzy', 17), ('Johnny', 13)]

Or, turn the list of dicts into a single key,value pair dictionary:

>>> {d['Name']:d['Age'] for d in thisismylist}
{'Johnny': 13, 'Albert': 16, 'Suzy': 17}

So, same method, a way to print them:

>>> print '\n'.join(d['Name'] for d in thisismylist)
Albert
Suzy
Johnny   

And you can print it sorted if you wish:

>>> print '\n'.join(sorted(d['Name'] for d in thisismylist))
Albert
Johnny
Suzy

Or, sort by their ages while flattening the list:

>>> for name, age in sorted([(d['Name'],d['Age']) for d in thisismylist],key=lambda t:t[1]):
...    print '{}: {}'.format(name,age)
... 
Johnny: 13
Albert: 16
Suzy: 17

Upvotes: 22

D.Shawley
D.Shawley

Reputation: 59633

You could project the Name attribute out of each element in the list and join the results with newlines:

>>> print '\n'.join(x['Name'] for x in thisismylist)
Albert
Suzy
Johnny

Edit

It took me a few minutes, but I remembered the other interesting way to do this. You can use a combination of itertools and the operator module to do this as well. You can see it on repl.it too.

>>> from itertools import imap
>>> from operator import itemgetter
>>> print '\n'.join(imap(itemgetter('Name'), thisismylist))
Albert
Suzy
Johnny

In any case, you are probably better off using a vanilla for loop, but I figured that some other options were in order.

Upvotes: 1

user2479509
user2479509

Reputation: 334

If you're just looking for values associated with 'Name', your code should look like:

for d in thisismylist:
    print d['Name']

Upvotes: 24

Henry Keiter
Henry Keiter

Reputation: 17188

Looks like you need to go over the Python flow-control documentation. Basically, you just loop over all the items in your list, and then for each of those items (dictionaries, in this case) you can access whatever values you want. The code below, for instance, will print out every value in every dictionary inside the list.

for d in my_list:
    for key in d:
        print d[key]

Note that this doesn't print the keys, just the values. To print the keys as well, make your print statement print key, d[key]. That easy!

But really, go read the flow-control documentation; it's very nice.

Upvotes: 13

Related Questions