How to remove duplicates from a Python List
- Previous Page Request Module
- Next Page Reverse String
Learn how to remove duplicates from a List in Python.
Instance
Remove any duplicates from the list:
mylist = ["a", "b", "a", "c", "c"] mylist = list(dict.fromkeys(mylist)) print(mylist)
Example Explanation
First, we have a List with duplicates:
List with duplicates
mylist = ["a", "b", "a", "c", "c"] mylist = list(dict.fromkeys(mylist)) print(mylist)
Create a dictionary using list items as keys. This will automatically remove any duplicates, as dictionaries cannot have duplicate keys.
Create Dictionary
mylist = ["a", "b", "a", "c", "c"] mylist = list( dict.fromkeys(mylist) ) print(mylist)
Then, convert the dictionary back to a list:
Converted to List
mylist = ["a", "b", "a", "c", "c"] mylist = list(dict.fromkeys(mylist)) print(mylist)
Now we have a List with no duplicates, which has the same order as the original List.
Print the list to demonstrate the result:
Print List
mylist = ["a", "b", "a", "c", "c"] mylist = list(dict.fromkeys(mylist)) print(mylist)
Create Function
If you want to have a function that can send a list and then return unique items, you can create a function and insert the code in the example above.
Instance
def my_function(x): return list(dict.fromkeys(x)) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Example Explanation
Create a function with List as a parameter.
Create Function
def my_function(x): return list(dict.fromkeys(x)) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Use this List item to create a dictionary as the key.
Create Dictionary
def my_function(x): return list( dict.fromkeys(x) ) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Convert Dictionary to List:
Convert to List
def my_function(x): return list( dict.fromkeys(x) ) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Return List:
Return List
def my_function(x): return list(dict.fromkeys(x)) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Call the function using a list as a parameter:
Call Function
def my_function(x): return list(dict.fromkeys(x)) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
Print Result:
Print Result
def my_function(x): return list(dict.fromkeys(x)) mylist = my_function(["a", "b", "a", "c", "c"]) print(mylist)
- Previous Page Request Module
- Next Page Reverse String