-1

input: [("abc", 1, "def"), ("abc", 1, "ghi"), ("bc", 2, "a"), ("bc", 2, "b"), ("bc", 3, "a")]

expected output: [("abc", 1, "def"), ("bc", 2, "a"), ("bc", 3, "a")]

I was trying something like:- field_list = [field for i, field in enumerate(field_list) for cmp_field in field_list[i+1:] if].....don't know how if would suit here?

I wanted to achieve this using list comprehension. Logic for getting output -- remove the duplicates(tuple is treated as duplicate if item[0] and item[1] are same).

I could achieve it using traditional for loops but I would like to get this with list comprehension. Any thoughts?

Edit: ("abc", 1, "def") and ("abc", 1, "ghi") are duplicates, so I can pick the first one.

9
  • 2
    Why should ("abc", 1 ,"ghi") not be in your output list?
    – mkrieger1
    May 13, 2015 at 11:13
  • Possible duplicate of this.
    – Ami Tavory
    May 13, 2015 at 11:13
  • 3
    I suggest you show us how you would achieve it using traditional for loops so that it becomes clear what you want to do.
    – mkrieger1
    May 13, 2015 at 11:15
  • because it is duplicate as per the "duplicate definition" for this question. ("abc", 1, "def") and ("abc", 1, "ghi") are duplicate. So pick either of them or the 1st one in output list. May 13, 2015 at 11:15
  • 3
    Use a set() to keep track of seen items, check this answer if you want to do this using a list comprehension. May 13, 2015 at 11:17

3 Answers 3

1

Taking inspiration from this, you might try

inp = [("abc", 1, "def"), ("abc", 1, "ghi"), ("bc", 2, "a"), ("bc", 2, "b"), ("bc", 3, "a")]
res = []
[res.append(el) for el in inp if not [tmp for tmp in res if tmp[0] == el[0] and tmp[1] == el[1]]]

Although I believe regular for loops would be better for your situation.

0
1
output = [(x, y, z) for j, (x, y, z) in enumerate(input) if (x, y) not in [(x2, y2) for x2, y2, _ in input[:j]]]
# output = [('abc', 1, 'def'), ('bc', 2, 'a'), ('bc', 3, 'a')]

However, it might be more efficient with a traditional for loop, since you would not need to build the second list at each iteration (or a set as suggested by Ashwini Chaudhary).

0
0

I used groupby which is an intermediate step. .

In [40]: l=[("abc", 1, "def"), ("abc", 1, "ghi"), ("bc", 2, "a"), ("bc", 2, "b"), ("bc", 3, "a")]

In [41]: from itertools import groupby

In [42]: groups=[list(g) for k,g in groupby(l,key=itemgetter(1))]

In [43]: groups
Out[43]: 
[[('abc', 1, 'def'), ('abc', 1, 'ghi')],
 [('bc', 2, 'a'), ('bc', 2, 'b')],
 [('bc', 3, 'a')]]

In [44]: [elem[0] for elem in groups]
Out[44]: [('abc', 1, 'def'), ('bc', 2, 'a'), ('bc', 3, 'a')]
5
  • 1
    That wouldn't work with [('abc', 1, 'def'), ('def', 1, 'abc')] May 13, 2015 at 11:33
  • before grouping we can sort the list 'l'
    – Ajay
    May 13, 2015 at 11:35
  • 1
    @Ajay: the OP says "tuple is treated as duplicate if item[0] and item[1] are same", not "only if item[1] is the same". Simply change your key. (PS: you could do next(g) instead of list(g)[0].)
    – DSM
    May 13, 2015 at 11:36
  • what would it change? for the input [('abc', 1, 'def'), ('def', 1, 'abc')], the output should be [('abc', 1, 'def'), ('def', 1, 'abc')] May 13, 2015 at 11:36
  • @DSM Please edit the answer.I didn't get what the questionnaire want and what people are expecting
    – Ajay
    May 13, 2015 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.