5

I'm trying to remove the duplicates out of tuples in a list and add them in a new list with out duplicates,

I tried to make two loops but and check for duplicates or sets but the problem there's three tuples

can anyone help me, I'm stuck here

example

[(2, 5), (3, 5), (2, 5)]

Output

[2, 3, 5]
DevLounge
  • 8,313
  • 3
  • 31
  • 44
Joe
  • 223
  • 1
  • 10
  • 1
    @ivan_pozdeev close, but OP's expected output isn't the same as the dupe. The dupe would prescribe `[(2, 5), (3, 5)]` – Adam Smith Apr 04 '16 at 19:31
  • @AdamSmith Ah, who cares. They are all about accumulating and uniquifying. Converting that to this is enough research to ask an OP for. – ivan_pozdeev Apr 04 '16 at 19:38
  • 3
    @ivan_pozdeev Because it's a different question? Lots of things are about accumulating and removing duplicates, they're not all the same. Similarly you could close as dupe a question about an implementation of QuickSort by pointing it at an implementation of BubbleSort. – Adam Smith Apr 04 '16 at 19:39
  • rolled back to initlal question – DevLounge Apr 04 '16 at 19:44
  • @Apero If this aspires to be a question separate from other generic "accumulate and filter" ones rather than a duplicate, at least the title should say how it's different. The current one is pretty generic. – ivan_pozdeev Apr 04 '16 at 19:48
  • Your title was way too complicated. Keep in mind that questions have to be found in search engines. I do not know a lot of people who would ask google about "accumulating and uniquifying". Many SO users are not native english speakers and would maybe search for `flatten list of tuples and remove duplicates` – DevLounge Apr 04 '16 at 19:51

4 Answers4

5

If order isn't important, you can make a set, add each element of each tuple to the set, and the set is your result.

s = set()
for tup in lst:
    for el in tup:
        s.add(el)
# or use a set comprehension:
# # s = {el for tup in lst for el in tup}

If order IS important, you can do mostly the same, but also have a result list to add to.

s = set()
result = []
for tup in lst:
    for el in tup:
        if el not in s:
            s.add(el)
            result.append(el)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • 1
    Or: `set.union(*map(set, lst))` -- for the lst report above, the result is set([2, 3, 5]) – F1Rumors Apr 04 '16 at 19:46
  • @F1Rumors, make this your own answer but do not post on someone else answer – DevLounge Apr 04 '16 at 19:49
  • 2
    @f1rumors yup! I tend to avoid map/reduce/filter solutions in python because of how much better comprehensions look, though – Adam Smith Apr 04 '16 at 19:58
  • 3
    @AdamSmith of course, `set.union(*[set(x) for x in lst])` is just as cool. @Apero not sure what your point is, as this comment was apropos of the set answer from Adam and a reflection of his code, in 2.6 comprehension/non loop format, not worthy of a whole answer to itself, nor something I wanted to edit over his answer... – F1Rumors Apr 04 '16 at 20:27
2

You can use set comprehension:

lst = [(2, 5), (3, 5), (2, 5)]
{e for l in lst for e in l}
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
nstoitsev
  • 740
  • 8
  • 7
2

you need to iter through each tuple and then each element of tuple. before append just check if the element is in list:

a = [(2, 5), (3, 5), (2, 5)]
b = []
for i in a:
    for j in i:
        if j not in b:
           b.append(j)
print b

After running above code I got this output:

[2, 5, 3]
DevLounge
  • 8,313
  • 3
  • 31
  • 44
Denis
  • 1,219
  • 1
  • 10
  • 15
  • 1
    Always use `x no in lst` instead of `not x in lst` – DevLounge Apr 04 '16 at 19:37
  • 1
    @denis no, because it's special-cased within Python (well CPython anyway) to by synonymous. `x not in y` is the preferred nomenclature. http://stackoverflow.com/questions/8738388/order-of-syntax-for-using-not-and-in-keywords – Adam Smith Apr 04 '16 at 19:40
  • no but it is way more readable and therefore easier to understand. IDEs like PyCharm, Intellij etc always recommend it as well. – DevLounge Apr 04 '16 at 19:40
1

An easy way to do so is using numpy ravel, and then set:

import numpy as np
lst = [(2, 5), (3, 5), (2, 5)]
res = list(set(np.ravel(a)))

gives:

[2, 3, 5]

Answer to Apero's comment:

if you don't want to use numpy, you would be able to flatten the list with:

lst = [(2,5), (3,5), (2,5)]
tmp = []
for i in lst:
    for j in i:
        tmp.append(j)

res = set(tmp)
print res

which gives:

[2, 3, 5] 
Alejandro
  • 3,263
  • 2
  • 22
  • 38
  • this involves using a third-party library while this can be done really simply with python stdlib IMO. – DevLounge Apr 04 '16 at 19:58
  • Your are right. However, this is a good and fast option. I have modified the example according to your comment. – Alejandro Apr 04 '16 at 20:26