0

I am trying to find unique values b/w 2 lists but this logic doesn't seems to work

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]
for element in f:
    if element in x:
        f.remove(element)
print f

desired output

[11, 22, 33, 44]

actual output

[11, 22, 33, 44, 4]

Get only unique elements from two lists python

same ask here solution:

x = [1,2,3,4]
f = [1,11,22,33,44,3,4]

res = list(set(x+f))
print(res)
[1, 2, 3, 4, 33, 11, 44, 22]

as you can see its adding 1,2,3,4 not output I need

Ahsan Naseem
  • 1,046
  • 1
  • 19
  • 38
  • 2
    The same question with the exact same sample data has already been asked and answered here: https://stackoverflow.com/questions/28444561/get-only-unique-elements-from-two-lists-python – Mike Scotty Jul 14 '17 at 08:26
  • https://stackoverflow.com/questions/28444561/get-only-unique-elements-from-two-lists-python question is same but output is different all answers over ther dont giving unique values but merging lists and using set() – Ahsan Naseem Jul 14 '17 at 08:27
  • 1
    I voted to reopen because, in contrast to the other question, this one stated the desired behavior which is different from the behavior in the answers given there. (There may be another dup, though.) – MB-F Jul 14 '17 at 08:32
  • 3
    there's a dupe somewhere no doubt, here's a not super efficient solution `[i for i in f if i not in x]` – Chris_Rands Jul 14 '17 at 08:32
  • I answered your original question ([clicky](https://stackoverflow.com/a/45098345/4349415)) - you should be more specific and edit the original question if it needs clarification instead of posting the same question again as a different user. – Mike Scotty Jul 14 '17 at 08:35
  • 1
    @mpf82 The question you answered was asked in 2015. I don't think it is by the same author. – MB-F Jul 14 '17 at 08:37
  • @kazemakase oh, my bad, I was only looking at the sample data, not at the date. – Mike Scotty Jul 14 '17 at 08:38
  • @mpf82 I used these values as sample code as that user did, and that page has not single verified answer and output and answers over there are completely different – Ahsan Naseem Jul 14 '17 at 08:39
  • 1
    @mpf82 I guess the OP read that question but did not find a satisfactory answer there, so asked it again in a different way. Seems legitimate to me. – MB-F Jul 14 '17 at 08:39
  • 2
    @AhsanNaseem Hint for the future: Indicate the research you did from the beginning. If you link to another question and explain why it is different for you we can avoid a lot of trouble :) – MB-F Jul 14 '17 at 08:41
  • @Chris_Rands thanks it helps alot! – Ahsan Naseem Jul 14 '17 at 08:41
  • @kazemakase thanks will do that :) – Ahsan Naseem Jul 14 '17 at 08:42
  • 1
    @kazemakase I agree so I reopened. – ayhan Jul 14 '17 at 08:59
  • @kazemakase thanks much appreciated time you put into this – Ahsan Naseem Jul 14 '17 at 09:14
  • @ayhan You can close it again if you want ;) https://stackoverflow.com/questions/41125909/python-find-elements-in-one-list-that-are-not-in-the-other – Chris_Rands Jul 14 '17 at 09:49

1 Answers1

5

After all the hassle with closing and re-opening I feel someone ought to actually answer the question.

There are different ways to achieve the desired result:

  1. List comprehensions: [i for i in f if i not in x]. Maybe less efficient but preserves order. Credit goes to Chris_Rands (comment above).

  2. Set operations: set(f) - set(x). Likely more efficient for larger lists but does not preserve order. Gredit goes to mpf82. This also removes duplicates in f, as pointed out by asongtoruin.

MB-F
  • 22,770
  • 4
  • 61
  • 116
  • There's another important comment to make about using sets - if an item appears twice in `f` but not at all in `x`, it will only appear once in the output list. The list comprehension would result in the item appearing twice. – asongtoruin Jul 14 '17 at 09:19
  • @asongtoruin good point. Thank you. – MB-F Jul 14 '17 at 09:20