5

I have a list of tuples like this: mylist = [(1,2,3),(6,1,1),(7,8,1),(3,4,5)]. If I use the list comprehension slist = [item for sublist in mylist for item in sublist], I could get slist = [1,2,3,6,1,1,7,8,1,3,4,5].

How should I modify if I need only unique elements in slist like this [1,2,3,6,7,8,4,5]?

bdhar
  • 21,619
  • 17
  • 70
  • 86

4 Answers4

11

Use a set instead of a list.

set(slist)

If you really need it as a list then you can convert it back to a list:

slist = list(set(slist))

Note that this conversion won't preserve the original order of the elements. If you need the same order you can use this instead:

>>> result = []
>>> seen = set()
>>> for innerlist in mylist:
        for item in innerlist:
            if not item in seen:
                seen.add(item)
                result.append(item)
>>> result
[1, 2, 3, 6, 7, 8, 4, 5]
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • May I ask a question here? What is the complexity of this operation `item in seen` (look up in `set` object)? Is it as effective as looking up in `dict` object? – ovgolovin Sep 29 '11 at 00:42
  • @ovgolovin: Yes, it should be about as fast as dict. – Mark Byers Sep 29 '11 at 00:46
  • 1
    Here it is. http://wiki.python.org/moin/TimeComplexity The complexities look the same, but for `set` they used the `Worst Case` term, and for `dict` they used the `Amortized Worst Case` term. I wonder why. – ovgolovin Sep 29 '11 at 00:51
8

You can actually make your first part a bit easier by using itertools.chain.from_iterable and then passing the result to set, which will only retain the unique elements:

>>> mylist = [(1,2,3),(6,1,1),(7,8,1),(3,4,5)]
>>> import itertools
>>> set(itertools.chain.from_iterable(mylist))
set([1, 2, 3, 4, 5, 6, 7, 8])
jterrace
  • 64,866
  • 22
  • 157
  • 202
2
import itertools
chain = itertools.chain(*mylist)
print(set(chain))

taken from Flattening a shallow list in Python and adapted for use in this question.

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
1

You should use python sets http://docs.python.org/library/sets.html

set(yourlist)

it will do the trick

wleao
  • 2,316
  • 1
  • 18
  • 17