How to Remove Duplicates From a Python List
Previous
Next
Learn how to remove duplicates from a List in Python. Show ExampleRemove any duplicates from a List:
mylist = ["a", "b", "a", "c", "c"]
Try it Yourself »
Example ExplainedFirst we have a List that contains duplicates: A List with Duplicates
mylist = ["a", "b", "a", "c", "c"]
Create a dictionary, using the List items as keys. This will automatically remove any duplicates because dictionaries cannot have duplicate keys. Create a Dictionary
mylist = ["a", "b", "a", "c", "c"]
Then, convert the dictionary back into a list: Convert Into a List
mylist = ["a", "b", "a", "c", "c"]
Now we have a List without any duplicates, and it has the same order as the original List. Print the List to demonstrate the result Print the List
mylist = ["a", "b", "a", "c", "c"]
Create a FunctionIf you like to have a function where you can send your lists, and get them back without duplicates, you can create a function and insert the code from the example above. Example
def my_function(x):
Try it Yourself »
Example ExplainedCreate a function that takes a List as an argument. Create a Function
def my_function(x):
Create a dictionary, using this List items as keys. Create a Dictionary
def my_function(x):
Convert the dictionary into a list. Convert Into a List
def my_function(x):
Return the list Return List
def my_function(x):
Call the function, with a list as a parameter: Call the Function
def my_function(x):
Print the result: Print the Result
def my_function(x):
Previous
Next
|