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.
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.
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.
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.
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.
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.
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.
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.
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}]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With