1

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 ?

  • 4
    `l = list(set(l))` Set is structure that allows only unique elements. – Poojan Nov 18 '19 at 17:29
  • If you search in your browser for "Python remove list duplicates", you'll find references that can explain this much better than we can manage here. – Prune Nov 18 '19 at 18:12

3 Answers3

1

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+)

https://stackoverflow.com/a/39980744/8692977

list({s:None for s in l}.keys())
Marsilinou Zaky
  • 1,038
  • 7
  • 17
0

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 list

As 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

  • We then take the keys from the new dictionary and convert back to a list
Jay
  • 2,535
  • 3
  • 32
  • 44
0

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)))
nosklo
  • 217,122
  • 57
  • 293
  • 297