So make a temporary dict with the key being the id. This filters out the duplicates. The values() of the dict will be the list

In Python2.7

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> {v['id']:v for v in L}.values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

In Python3

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> list({v['id']:v for v in L}.values())
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

In Python2.5/2.6

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> dict((v['id'],v) for v in L).values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
Answer from John La Rooy on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python-unique-dictionary-filter-in-list
Unique Dictionary Filter in List – Python | GeeksforGeeks
February 13, 2025 - Each dictionary is converted to a frozenset of its key-value pairs, creating a hashable representation that allows duplicates to be removed using a set. Unique frozenset items are converted back to dictionaries and result is stored in u ensuring list contains only unique dictionaries. json.dumps converts dictionaries to JSON strings providing a hashable and comparable format. By storing these strings in a set duplicates can be filtered and unique strings are then converted back to dictionaries.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-filter-list-of-dictionaries
How to filter a List of Dictionaries in Python | bobbyhadz
Copied!list_of_dictionaries = [ {'id': 1, 'name': 'alice'}, {'id': 2, 'name': 'bobby'}, {'id': 3, 'name': 'carl'}, ] list_of_values = [1, 3] filtered_list_of_dicts = [] for dictionary in list_of_dictionaries: if dictionary['id'] in list_of_values: filtered_list_of_dicts.append(dictionary) # 👇️ [{'id': 1, 'name': 'alice'}, {'id': 3, 'name': 'carl'}] print(filtered_list_of_dicts) ... We used a for loop to iterate over the list of dictionaries. On each iteration, we check if a certain condition is met. If the condition is met, we append the current dictionary to a new list. To filter a list of dictionaries by unique values:
Discussions

python - List of dictionaries that should be filtered by certain values - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
how to uniqify a list of dict in python - Stack Overflow
@Artsimon: Mine for instance creates ... n dictionaries. Yours is the same except that it doesn't use generators and iterators when it could. ... @delnan: thank you for providing details. i am only starting my way in python(especially in understanding differences between iterators and generator), so any comments are very appreciated and always helpful. – Artsiom Rudzenka Commented Jun 8, 2011 at 16:00 ... Find the answer to your question by ... More on stackoverflow.com
🌐 stackoverflow.com
August 6, 2011
Python filter dictionary unique values in a key and create another filtered dictionary - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
dictionary - Python filtering list of objects by distinct attribute - Stack Overflow
I have a list of objects with multiple attributes. I want to filter the list based on one attribute of the object (country_code), i.e. Current list elems = [{'region_code': 'EUD', 'country_code':... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
371

So make a temporary dict with the key being the id. This filters out the duplicates. The values() of the dict will be the list

In Python2.7

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ]
>>> {v['id']:v for v in L}.values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

In Python3

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> list({v['id']:v for v in L}.values())
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]

In Python2.5/2.6

>>> L=[
... {'id':1,'name':'john', 'age':34},
... {'id':1,'name':'john', 'age':34},
... {'id':2,'name':'hanna', 'age':30},
... ] 
>>> dict((v['id'],v) for v in L).values()
[{'age': 34, 'id': 1, 'name': 'john'}, {'age': 30, 'id': 2, 'name': 'hanna'}]
2 of 16
127

The usual way to find just the common elements in a set is to use Python's set class. Just add all the elements to the set, then convert the set to a list, and bam the duplicates are gone.

The problem, of course, is that a set() can only contain hashable entries, and a dict is not hashable.

If I had this problem, my solution would be to convert each dict into a string that represents the dict, then add all the strings to a set() then read out the string values as a list() and convert back to dict.

A good representation of a dict in string form is JSON format. And Python has a built-in module for JSON (called json of course).

The remaining problem is that the elements in a dict are not ordered, and when Python converts the dict to a JSON string, you might get two JSON strings that represent equivalent dictionaries but are not identical strings. The easy solution is to pass the argument sort_keys=True when you call json.dumps().

EDIT: This solution was assuming that a given dict could have any part different. If we can assume that every dict with the same "id" value will match every other dict with the same "id" value, then this is overkill; @gnibbler's solution would be faster and easier.

EDIT: Now there is a comment from André Lima explicitly saying that if the ID is a duplicate, it's safe to assume that the whole dict is a duplicate. So this answer is overkill and I recommend @gnibbler's answer.

🌐
TestDriven.io
testdriven.io › tips › b4e36507-fd5e-4bbd-abe2-c2530e8c044f
Tips and Tricks - Get the unique values from a list of dictionaries | TestDriven.io
You can use a dictionary comprehension to create a list of dictionaries unique by value on a selected key · users = [ {"name": "John Doe", "email": "[email protected]"}, {"name": "Mary Doe", "email": "[email protected]"}, {"name": "Mary A. ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-unique-dictionary-filter-in-list
Unique Dictionary Filter in List - Python - GeeksforGeeks
July 12, 2025 - Each dictionary is converted to a frozenset of its key-value pairs, creating a hashable representation that allows duplicates to be removed using a set. Unique frozenset items are converted back to dictionaries and result is stored in u ensuring list contains only unique dictionaries. json.dumps converts dictionaries to JSON strings providing a hashable and comparable format. By storing these strings in a set duplicates can be filtered and unique strings are then converted back to dictionaries.
🌐
GeeksforGeeks
geeksforgeeks.org › python-get-unique-values-from-list-of-dictionary
Get Unique Values from List of Dictionary | GeeksforGeeks
February 4, 2025 - set is a built-in Python data structure that only allows unique elements and we can use it to collect unique values from a list of dictionaries by iterating over the dictionaries and adding the values to the set.
🌐
YouTube
youtube.com › vlogize
How to filter a list of dictionaries by unique values and lowest scores in Python - YouTube
Learn how to effectively filter a list of dictionaries in Python based on unique names and their lowest scores without using external libraries like Pandas.-...
Published   March 22, 2025
Views   0
🌐
GeeksforGeeks
geeksforgeeks.org › python-unique-dictionary-filter-in-list › amp
Python | Unique dictionary filter in list - GeeksforGeeks
This method uses list comprehension to filter unique dictionaries and the dict() function to convert the filtered list of tuples to a list of dictionaries.
Find elsewhere
🌐
30 Seconds of Code
30secondsofcode.org › home › python › filter unique list values
Python - Filter unique list values - 30 seconds of code
June 14, 2024 - from collections import Counter def filter_unique(lst): return [item for item, count in Counter(lst).items() if count > 1] filter_unique([1, 2, 2, 3, 4, 4, 5]) # [2, 4]
🌐
AskPython
askpython.com › python › dictionary › filter-list-of-dictionaries-based-on-key-values
3 Ways to Filter List of Dictionaries Based on Key Values - AskPython
April 27, 2023 - To conclude we have learned what is a dictionary and how a dictionary stores the data in the form of key-value pairs where the keys should be unique and the values may not be unique. We have seen a few operations on the dictionary like creating a dictionary, printing the keys and values of the dictionary, deleting a certain key from the dictionary, and removing the last key-value pair from the dictionary. Coming to filtering the dictionary based on the key values, we have seen three approaches. Firstly, we created a list of dictionaries according to the syntax and used list comprehension to set a condition and print the filtered list of dictionaries that satisfy the given condition.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-get-unique-values-from-list-of-dictionary
Get Unique Values from List of Dictionary - Python
July 12, 2025 - set is a built-in Python data structure that only allows unique elements and we can use it to collect unique values from a list of dictionaries by iterating over the dictionaries and adding the values to the set.
Top answer
1 of 2
1

With all of the pairs appearing in the list, the implementation should be simple enough.

Sort the list. The "lower" labels will come to the front. In particular, the first entry will be a viable main element. Put every such link into the results list, keeping track of the second elements. Then pass over the links starting with "other" names. Repeat this until you've covered the entire original list. Here's a painfully detailed version:

start = [
    { 'name00': 'test1', 'name01': 'test2' },
    { 'name00': 'test1', 'name01': 'test3' },
    { 'name00': 'test2', 'name01': 'test1' },
    { 'name00': 'test2', 'name01': 'test3' },
    { 'name00': 'test3', 'name01': 'test1' },
    { 'name00': 'test3', 'name01': 'test2' },
    { 'name00': 'test4', 'name01': 'test5' },
    { 'name00': 'test5', 'name01': 'test4' }
]

result = []

start.sort(key=lambda link: link["name00"]+link["name01"]) 

for link in start:
    print(link)

link_key = None
pos = 0
while pos < len(start):

    other_name = []

    link = start[pos]
    if not link_key:
        link_key = link['name00']

    # Gather all of the links that start with the lowest name.
    # Keep track of the other names for later use.
    while start[pos]['name00'] == link_key:
        link = start[pos]
        result.append(link)
        other_name.append(link["name01"])
        pos += 1

    # Now is "later" ... ignore all links that start with other names.
    while pos < len(start) and              \
          start[pos]['name00'] in other_name:
        link = start[pos]
        pos += 1

    link_key = None


# Print the resulting pairs
for link in result:
    print(link["name00"], link["name01"])

Output:

test1 test2
test1 test3
test4 test5
2 of 2
0

Not sure, but here is what I understood:

two items i and j from results are considered equal if:

  • (i['name00'] == j['name00'] and i['name01'] == j['name01']) or
  • (i['name01'] == j['name00'] and i['name00'] == j['name01']).

Is it right?

In that case, you can define a key which compares the values 'name00'/'name01' by sorting them, then use that key to store all the items in a dictionary to eliminate duplicates.

For instance:

def key(item):
    return frozenset([item['name00'], item['name01']])


by_key = {key(item): item for item in results}

pprint.pprint(sorted(by_key.values()))

You get:

[{'name00': 'test2', 'name01': 'test1'},
 {'name00': 'test3', 'name01': 'test1'},
 {'name00': 'test3', 'name01': 'test2'},
 {'name00': 'test5', 'name01': 'test4'}]
🌐
TutorialsPoint
tutorialspoint.com › python-dictionaries-with-unique-value-lists
Python – Dictionaries with Unique Value Lists
When it is required to get the dictionaries with unique value lists, the ‘set’ operator and the list methods are used, along with a simple iteration. ... my_dictionary = [{'Python' : 11, 'is' : 22}, {'fun' : 11, 'to' : 33}, {'learn' : 22},{'object':9},{'oriented':11}] print("The dictionary is : " ) print(my_dictionary) my_result = list(set(value for element in my_dictionary for value in element.values())) print("The resultant list is : ") print(my_result) print("The resultant list after sorting is : ") my_result.sort() print(my_result)
🌐
Finxter
blog.finxter.com › home › learn python blog › how to filter a list of dictionaries in python?
How to Filter a List of Dictionaries in Python? - Be on the Right Side of Change
April 17, 2020 - In this article, you’ve learned how to filter a list of dictionaries easily with a simple list comprehension statement. That’s far more efficient than using the filter() method proposed in many other blog tutorials. Guido, the creator of Python, hated the filter() function!
🌐
LearnPython.com
learnpython.com › blog › filter-dictionary-in-python
How to Filter a Python Dictionary | LearnPython.com
December 26, 2022 - We can apply the same basic logic in order to filter dictionaries in Python. There are only a few differences from the example in the previous section: Instead of elements in a list, we need to iterate over the key-value pairs of the dictionary. We can do this by using the dictionary dict.items() ...