How to Remove Duplicates from a List of Dictionaries in Python?

Estimated read time 2 min read

To remove duplicates from a list of dictionaries in Python, you can use a combination of the set(), tuple(), and sorted() functions along with a list comprehension. Here is an example code snippet that removes duplicates from a list of dictionaries:

original_list = [
    {"name": "John", "age": 25},
    {"name": "Jane", "age": 30},
    {"name": "John", "age": 25},
    {"name": "Adam", "age": 22},
    {"name": "Jane", "age": 30},

# Remove duplicates from the list of dictionaries
new_list = [dict(t) for t in set([tuple(d.items()) for d in original_list])]

print(new_list)  # Output: [{'name': 'Jane', 'age': 30}, {'name': 'John', 'age': 25}, {'name': 'Adam', 'age': 22}]

In this code, we define an original list of dictionaries (original_list) that contains duplicate dictionaries.

Next, we create a new list of dictionaries (new_list) using a list comprehension that removes duplicates from the original list. We first convert each dictionary to a list of key-value pairs using the items() method, and then convert each list to a tuple using the tuple() function. We then use the set() function to remove duplicates from the list of tuples. Finally, we convert each tuple back to a dictionary using the dict() function.

Finally, we print the new list of dictionaries (new_list) that contains unique dictionaries.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply