Luke Williams
Luke Williams

Reputation: 81

How to choose certain dictionary keys ,and then move the whole dictionary to a separate list

I have a list of dictionaries

[{country : UK, age : no20, mode : 1}, {country : USA, age: 25, mode : 0}, 
 {country : France, age : 23, mode : 1}]

I would like to take the two dictionaries with only 'mode : 0' and then store them in a separate list / dictionary

Any help is appreciated

Thanks :)

Upvotes: 2

Views: 40

Answers (1)

Synthaze
Synthaze

Reputation: 6090

So first your example of a list of dict is wrong most likely. Keys and some values are string and so you must quote them.

Otherwise this does what you want:

lst = [{'country' : 'UK', 'age' : 20, 'mode' : 1}, {'country' : 'USA', 'age': 25, 'mode' : 0}, {'country' : 'France', 'age' : 23, 'mode' : 1}]

print([x for x in lst if x['mode'] == 0])

Upvotes: 2

Related Questions