I have a list and if there is 2 elements with the same values i want to keep only one
l = [1,2,3,2,2]
and I want the output to be:
[1,2,3]
how do i do that ?
I have a list and if there is 2 elements with the same values i want to keep only one
l = [1,2,3,2,2]
and I want the output to be:
[1,2,3]
how do i do that ?
If you want to keep the order of elements you can go for this approach
result = []
l = [1,2,3,2,2]
for e in l:
if e not in result:
result.append(e)
print(result)
# Result: [1, 2, 3]
If the order is not important you can do it in one line as mentioned before
list(set(l))
Another one-line solution that will keep the order of elements is (Note dictionary hold order of elements in python 3.6+)
list({s:None for s in l}.keys())
you can use set()
and list()
:
list(set([1,2,3,2,2]))
# Output: [1, 2, 3]
set()
removes all duplicates, while not necessarily preserving order.list()
converts the set {1, 2, 3}
back to a listAs dictionaries are ordered in later versions of Python, this:
list(dict.fromkeys([1,2,3,2,2]).keys())
# Output: [1, 2, 3]
will preserve order on Python 3.7+, unlike set()
.
Note:
dict.fromkeys([1,2,3,2,2])
# Output: {1: None, 2: None, 3: None}
We generate a (order sensitive) dictionary, using the list items as keys.
Dictionary keys, like sets, are hashed and so unique - all repetition in the list is discarded
If you want to preserve the items from the original list in the same order, use the unique_everseen
recipe from itertools
documentation:
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
seen_add = seen.add
if key is None:
for element in filterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
Usage:
print(list(unique_everseen(l)))