28

Suppose you have a list of dictionaries like this one:

a = [ {'name':'pippo', 'age':'5'} , {'name':'pluto', 'age':'7'} ]

What do you to extract from this list only the dict where name==pluto? To make things a little bit harder, consider that I cannot do any import

2
  • what do you want to extract? :/ Oct 26, 2011 at 9:31
  • the dictionary, i.e. {'name':'pluto', 'age':'7'} Oct 26, 2011 at 9:40

5 Answers 5

41

List comprehension is ideal for this:

[d for d in a if d['name'] == 'pluto']
5
  • 4
    It's 'if' not 'where'. Edit: I see you fixed it. It's worth mentioning that this is still a list, as there is a potential for multiple results with that value - if you are sure there is only one and only want one, just access the element in position 0 on that new list. Oct 26, 2011 at 9:33
  • @Lattyware Yeah, I edited it as soon as I saw it. You were quick to notice :) And yes, it will produce a list. Using [0] directly is dangerous though if the result will be an empty list. You can use something like x = x[0] if x else {} on the result. Or if the name is unique then [x] = [d for d in a if d['name'] == 'pluto'] or [{}]
    – rplnt
    Oct 26, 2011 at 9:51
  • @rplnt Indeed, that's why I said 'if you are sure there is one, and only one' - if there are none, or more than one, you are going to end up with problems, albeit problems you have now given elegant solutions to. Thanks for clarifying. Oct 31, 2011 at 12:20
  • 1
    Is there any special reason as to why there's a need of using [.........] as the output parethesis Oct 7, 2017 at 18:57
  • @PranjalGupta In what example? On the right side, it's a syntax for list comprehension (check documentation). On the lest side ([x] = ...) it means that we will extract items from collection on the right side. Imagine that n = [1] then [x] = n and that is equal to x = n[0] resulting in x = 1
    – rplnt
    Oct 8, 2017 at 14:05
14

Apart from list comprehension that other responses give it to you, you can also do it with a filter and a lambda:

filter(lambda x: x.get('name') == 'pluto',a)
4
>>> [d['age'] for d in a if d['name']=='pluto']
['7']
1
  • 1
    Given they didn't specify, one would presume they want to extract the whole dict, but if they want something in particular, then this is a good solution. Oct 26, 2011 at 9:35
3

Use a list comprehension which picks out the correct dict in the list of dicts.

 >>> [d for d in a if d['name']=='pluto']
 [{'age': '7', 'name': 'pluto'}]

Or, if you're not sure if all of the dicts in a have a 'name' key,

 >>> [d for d in a if d.get('name')=='pluto']
 [{'age': '7', 'name': 'pluto'}]

Note that both of these return a list. If you know that there is only one matching entry, you can add [0] to the end to return the actual dict:

 >>> [d for d in a if d['name']=='pluto'][0]
 {'age': '7', 'name': 'pluto'}
1
[d for d in a if d['name'] == 'pluto']

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.