Remove duplicates from a list in Python

I am going straight to the point

my_list = [1, 2, 3, 4, 4, 5, 2]

How do we remove duplicate no. or elements from the list.

Simple, we have 3 easy method.

Using set

my_list = [1, 2, 3, 4, 4, 5, 2]
updated_list = list(set(my_list))
print(…


This content originally appeared on DEV Community and was authored by Aditya Priyadarshi

I am going straight to the point

my_list = [1, 2, 3, 4, 4, 5, 2]

How do we remove duplicate no. or elements from the list.

Simple, we have 3 easy method.

  1. Using set
my_list = [1, 2, 3, 4, 4, 5, 2]
updated_list = list(set(my_list))
print(updated_list)

#The output will be: [1, 2, 3, 4, 5]

As we know that set doesn't support duplicate, we can simply use this method to remove duplicates. Here I have type casted the list into a set and then again type casted in to list.

  1. Using dict
my_list = [1, 2, 3 , 4, 4, 5, 2]
updated_list = list(dict.fromkeys(my_list))
print(mylist)

#The output will be: [1, 2, 3, 4, 5]

Well dict also doesn't support duplicates so when we convert the list to dict, so we are creating dict from the list and then converting it back to list.

  1. I don't know what to call this method
my_list = [1, 2, 3 , 4, 4, 5, 2]
updated_list = []
for item in my_list:
    if item not in list:
        updated_list.append(item)
my_list = updated_list
print(my_list)

#The output will be: [1, 2, 3, 4, 5]

So that was 3 ways to remove duplicates from a list.
I you got some more methods write down in the comment box below, I would love to see them, until then bey bye and

Peace ✌


This content originally appeared on DEV Community and was authored by Aditya Priyadarshi


Print Share Comment Cite Upload Translate Updates
APA

Aditya Priyadarshi | Sciencx (2021-10-15T04:18:21+00:00) Remove duplicates from a list in Python. Retrieved from https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/

MLA
" » Remove duplicates from a list in Python." Aditya Priyadarshi | Sciencx - Friday October 15, 2021, https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/
HARVARD
Aditya Priyadarshi | Sciencx Friday October 15, 2021 » Remove duplicates from a list in Python., viewed ,<https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/>
VANCOUVER
Aditya Priyadarshi | Sciencx - » Remove duplicates from a list in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/
CHICAGO
" » Remove duplicates from a list in Python." Aditya Priyadarshi | Sciencx - Accessed . https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/
IEEE
" » Remove duplicates from a list in Python." Aditya Priyadarshi | Sciencx [Online]. Available: https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/. [Accessed: ]
rf:citation
» Remove duplicates from a list in Python | Aditya Priyadarshi | Sciencx | https://www.scien.cx/2021/10/15/remove-duplicates-from-a-list-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.