Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to uniqify a list of dict in python

Tags:

python

I have a list:

d = [{'x':1, 'y':2}, {'x':3, 'y':4}, {'x':1, 'y':2}] 

{'x':1, 'y':2} comes more than once I want to remove it from the list.My result should be:

 d = [{'x':1, 'y':2}, {'x':3, 'y':4} ] 

Note: list(set(d)) is not working here throwing an error.

like image 412
ramesh.c Avatar asked Jun 08 '11 15:06

ramesh.c


People also ask

How do I convert a list of dictionaries in Python?

Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

How do I iterate through a dictionary list?

In Python, to iterate the dictionary ( dict ) with a for loop, use keys() , values() , items() methods. You can also get a list of all keys and values in the dictionary with those methods and list() . Use the following dictionary as an example. You can iterate keys by using the dictionary object directly in a for loop.

Can you nest a dictionary in a list Python?

You can have dicts inside of a list. The only catch is that dictionary keys have to be immutable, so you can't have dicts or lists as keys.

How to access dictionary of a list in Python?

In the following program, we create a list of length 3, where all the three elements are of type dict. Each element of the list is a dictionary. Dictionary is like any element in a list. Therefore, you can access each dictionary of the list using index.

How to remove duplicates from a list of dictionaries in Python?

The append function is used to insert a new value in the list of dictionaries, we will use pop () function along with this to eliminate the duplicate data. This function is used to insert updated data based on an index. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

Can you put a dict in a set of strings?

However, since a dict is unhashable (can't be put in a set), I provided a workaround. First, the dicts are converted to strings, placed in a set (to keep the unique ones), and then converted back to dicts using ast.literal_eval.

What is the best alternative to Python dictionary?

As an alternative we can use flatdict, which is much more lightweight and battle-tested. The library is very versatile and also enables us to use custom separators. However, one of the best features it provides is the ability to access the newly created dictionary as before – that is, you can access values using either the new keys or the old ones.


2 Answers

If your value is hashable this will work:

>>> [dict(y) for y in set(tuple(x.items()) for x in d)] [{'y': 4, 'x': 3}, {'y': 2, 'x': 1}] 

EDIT:

I tried it with no duplicates and it seemed to work fine

>>> d = [{'x':1, 'y':2}, {'x':3, 'y':4}] >>> [dict(y) for y in set(tuple(x.items()) for x in d)] [{'y': 4, 'x': 3}, {'y': 2, 'x': 1}] 

and

>>> d = [{'x':1,'y':2}] >>> [dict(y) for y in set(tuple(x.items()) for x in d)] [{'y': 2, 'x': 1}] 
like image 114
GWW Avatar answered Sep 30 '22 19:09

GWW


Avoid this whole problem and use namedtuples instead

from collections import namedtuple  Point = namedtuple('Point','x y'.split()) better_d = [Point(1,2), Point(3,4), Point(1,2)] print set(better_d) 
like image 45
Jochen Ritzel Avatar answered Sep 30 '22 17:09

Jochen Ritzel