0% found this document useful (0 votes)
130 views

How To Remove Duplicates From A Python List

This document discusses how to remove duplicate elements from a Python list. It provides an example of converting a list into a dictionary, which removes duplicates, and then converting it back into a list. It also shows how to create a function that takes a list as a parameter and returns the list without duplicates. The function uses the same process of converting to a dictionary and back to remove any duplicate elements.

Uploaded by

Darshan M M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

How To Remove Duplicates From A Python List

This document discusses how to remove duplicate elements from a Python list. It provides an example of converting a list into a dictionary, which removes duplicates, and then converting it back into a list. It also shows how to create a function that takes a list as a parameter and returns the list without duplicates. The function uses the same process of converting to a dictionary and back to remove any duplicate elements.

Uploaded by

Darshan M M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

How to Remove Duplicates From a

Python List
❮ PreviousNext ❯

Learn how to remove duplicates from a List in Python.

Example
Remove any duplicates from a List:

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

Try it Yourself »

Example Explained
First we have a List that contains duplicates:

A List with Duplicates


mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

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"]
mylist = list( dict.fromkeys(mylist) )
print(mylist)

Then, convert the dictionary back into a list:


Convert Into a List
mylist = ["a", "b", "a", "c", "c"]
mylist = list( dict.fromkeys(mylist) )
print(mylist)

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"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

Create a Function
If 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):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

Try it Yourself »

Example Explained
Create a function that takes a List as an argument.

Create a Function
def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)
Create a dictionary, using this List items as keys.

Create a Dictionary
def my_function(x):
  return list( dict.fromkeys(x) )

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

Convert the dictionary into a list.

Convert Into a List


def my_function(x):
  return list( dict.fromkeys(x) )

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

Return the 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, with a list as a parameter:

Call the Function


def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

Print the result:

Print the Result


def my_function(x):
  return list(dict.fromkeys(x))
mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

You might also like