Question #764

In Python, how to remove duplicates from lists ?

Answer

The best option to remove duplicates from a list is to convert the list in set (which cannot contain duplicate), then to convert back to list:

myList = [5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]
# list -> set -> list
myList = list(set(myList))
# [1, 2, 3, 4, 5, 6]
print (myList)

Set if one of the 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

5 events in history
Answer by Alphonsio 03/27/2021 at 10:23:01 AM

The best option to remove duplicates from a list is to convert the list in set (which cannot contain duplicate), then to convert back to list:

myList = [5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]
# list -> set -> list
myList = list(set(myList))
# [1, 2, 3, 4, 5, 6]
print (myList)

Set if one of the 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

Answer by Alphonsio 03/27/2021 at 10:18:47 AM

The best option to remove duplicates from a list is to convert the list in set (which cannot contain duplicate), then to convert back to list:

myList = [5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]
# list -> set -> list
myList = list(set(myList))
# [1, 2, 3, 4, 5, 6]
print (myList)

Set if one of the 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

Answer by Alphonsio 03/27/2021 at 10:18:40 AM

The best option to remove duplicates from a list is to convert the list in set (which cannot contain duplicate), thus to convert back to list:

myList = [5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]
# list -> set -> list
myList = list(set(myList))
# [1, 2, 3, 4, 5, 6]
print (myList)

Set if one of the 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

Answer by Alphonsio 03/27/2021 at 10:16:51 AM

The best option to remove duplicates from a list is to convert the list in set (which cannot contain duplicate), thus to convert back to list:

myList = [5, 1, 1, 2, 5, 6, 3, 4, 4, 4, 2]
# list -> set -> list
myList = list(set(myList))
# [1, 2, 3, 4, 5, 6]
print (myList)

Set if one of the 4 built-in data types in Python used to store collections of data, the other 3 are ListTuple, and Dictionary, all with different qualities and usage.

Question by Alphonsio 03/27/2021 at 10:14:56 AM
In Python, how to remove duplicates from lists ?
# ID Query URL Count

Icons proudly provided by Friconix.