I have some data on a dictionary. Each key in the dictionary has several values, one of them is called 'state' which can be equal to Georgia, Washington, etc. I want to get the unique states. I tried doing this but I get an incorrect syntax error
s = set( value for key in r value = key['state'] )
How can I get all the states?
Edit:
The data structure I have is actually a list of dictionaries so I want to get the values of r[0]['state'], r[1]['state'], etc and make an unique list.
Based on your comment since you have a list of dictionaries you can use map
function with passing dict.get
method to it, to get all state
values then you can loop over the values within a set
:
s = set( value for value in map(lambda x:x.get('state'),r))
Or for get ride of lambda
function you can use operator.itemgetter
:
from operator import itemgetter
s = set( value for value in map(itemgetter('state'),r))