Question #437   Submitted by Answiki on 03/27/2021 at 10:14:56 AM UTC

In Python, how to remove duplicates from lists ?

Answer   Submitted by Answiki on 03/27/2021 at 10:23:01 AM UTC

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 Answiki on 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 Answiki on 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 Answiki on 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 Answiki on 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 Answiki 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.