Question

How to remove duplicates from a list?

Updated on June 2, 2025 | By Learnzy Admin | 👁️ Views: 192 students

Solution
✔ Verified

Method 1:   list(set(my_list))
Removes duplicates, but order is not preserved.

my_list = [1, 2, 2, 3, 1]
unique_list = list(set(my_list))
print(unique_list)  # Output: [1, 2, 3] (order may change)

Method 2:   list(dict.fromkeys(my_list))
Removes duplicates and keeps original order.

my_list = [1, 2, 2, 3, 1]
unique_list = list(dict.fromkeys(my_list))
print(unique_list)  # Output: [1, 2, 3]
Was this solution helpful? 38
Click here to download practice questions on Python Lists

More Questions on Python Lists

Question 1

How to check if two lists are equal?


View solution
Question 2

Find common elements in two lists


View solution
Question 3

Is Python list mutable?


View solution
Question 4

Difference between sort() and sorted()?


View solution
Question 5

What is a list in Python?


View solution
Question 6

How to remove an element from a list?


View solution
Question 7

What is the difference between append() and extend()?


View solution