Lets say I have a list of dictionaries:
[{'county': 'Lincoln County', 'state': 'SD', 'fips': '46083'}, {'county': 'Minnehaha County', 'state': 'SD', 'fips': '46099'}, {'county': 'Minnehaha County', 'state': 'SD', 'fips': '46099'},...]
In the above example, 2 of the dictionaries in this list are the same. What I want to do is check and see, by the fips
key if the value. I know that I can use something like this to check in a dictionary, and in essence, create a new list of only unique entries:
result = {}
for key,value in dictionary.items():
if value not in result.values():
result[key] = value
print result
But I am having difficulty apply this to a list of dictionaries. What am I doing wrong here?
for i in dictionary:
for key,value in dictionary[i].items():
if value not in result.values():
result[key] = value
You can use a check flag.
Ex:
d = [{'county': 'Lincoln County', 'state': 'SD', 'fips': '46083'}, {'county': 'Minnehaha County', 'state': 'SD', 'fips': '46099'}, {'county': 'Minnehaha County', 'state': 'SD', 'fips': '46099'}]
check_val = set()
res = []
for i in d:
if i["fips"] not in check_val:
res.append(i)
check_val.add(i["fips"])
print(res)
Output:
[{'county': 'Lincoln County', 'state': 'SD', 'fips': '46083'}, {'county': 'Minnehaha County', 'state': 'SD', 'fips': '46099'}]