-1

I've searched all around the web but couldn't find the answer. There are two lists like this:

A = [1,2,3,4,3,2,1]
B = [1,2,3]

I wanna get output like this:

Out = [4,3,2,1]

First match of duplicate values will remove. SUGGESTED LINK DIDN'T SOLVE MY PROBLEM. If I use that, output will be:

Out =[4]
Gone
  • 1
  • 3
  • 1
    This example is not enough, what happens in case of `A=[1,2,3,1,4,3,2,1]` and `B=[1,2,1]`? – Tarun Lalwani Sep 05 '19 at 10:22
  • It would be [3,4,3,2,1] wanna delete first match. AND MY QUESTION ISN'T DUPLICATE – Gone Sep 05 '19 at 10:27
  • what happens in case of A = [1,2,1] and B = [1,2,3,1,4,3,2,1] – Vashdev Heerani Sep 05 '19 at 11:05
  • Does the order have to match? If `A = [1, 2, 3, 4]` does `B = [3, 2, 1]` behave differently from `B = [1, 2, 3]`? – ShadowRanger Sep 05 '19 at 22:07
  • @Darkknight: Unless there is some subtlety you haven't explained, your question is a duplicate; the linked duplicate also wants to be able to remove exactly one of each copy of a value in one `list` from another `list`. You just need to read more of the answers, e.g. [this one](https://stackoverflow.com/a/20259489/364696). – ShadowRanger Sep 06 '19 at 18:19
  • FYI, I did add [an answer](https://stackoverflow.com/a/57827145/364696) to [Python list subtraction operation](https://stackoverflow.com/q/3428536/364696) that is order-preserving, cares about element counts, and is `O(m + n)` (rather than `O(m * n)` like the other order-preserving, count-sensitive answers). – ShadowRanger Sep 06 '19 at 18:54

3 Answers3

3

Loop through the second list and remove the first one of each in B:

out = A[:]  # If you don't care about A being changed, this isn't needed

for b in B:
   out.remove(b)

Now out contains what you need. Remove() removes the first item that matches.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • what if A = [1,2,1] and B = [1,2,3,1,4,3,2,1] – Vashdev Heerani Sep 05 '19 at 11:01
  • That raises a ValueError, which doesn't seem unreasonable to me. – RemcoGerlich Sep 05 '19 at 11:12
  • This does `O(m * n)` work (`m` being length of `B`, `n` length of `A`), which is sub-optimal; for large inputs, building a new output by converting `B` to something like `collections.Counter`, then making a new `list` from `A` by `append`ing to the new `list` only if the count for the value is 0, otherwise decrementing the count, would reduce the work to `O(m + n)`. – ShadowRanger Sep 05 '19 at 22:11
  • @ShadowRanger: but isn't appending to a list linear, so that that would be quadratic if all elements of A are appended one by one? – RemcoGerlich Sep 06 '19 at 07:35
  • @RemcoGerlich: It's amortized `O(1)`; some appends require a resize, but it overallocates by a multiple of the current size, so most do not require a resize, they just put the pointer in an existing slot and increment the logical length. – ShadowRanger Sep 06 '19 at 18:52
  • @RemcoGerlich: I posted [an answer](https://stackoverflow.com/a/57827145/364696) that demonstrates the approach I described in detail, with explanation if you want to check it. – ShadowRanger Sep 06 '19 at 18:56
0
A = [1,2,1]
B = [1,2,3,1,4,3,2,1]
for b in B:
    if b in A:
        A.remove(b)
        B.remove(b)
print(A+B)

This is waht I suggest!

Vashdev Heerani
  • 660
  • 7
  • 21
-1

Try this:

Out = list(set(A)^set(B))

Full solution:

A = [1,2,3,4,3,2,1]
B = [1,2,3]
Out = list(set(A)^set(B))
print (Out)

To print in sorted order:

print (sorted(Out))

OR

print (list(set(A).difference(B)))
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shamsheer
  • 734
  • 4
  • 8
  • This will return only 4. 'cause if an object repeats in a set, it doesn't make diffrance. Than {4}=={4,4,4,4,4}. – Gone Sep 06 '19 at 07:19
  • I asked this question myself :) you mean I don't know what I asked? :) – Gone Sep 06 '19 at 07:30
  • Even if this were correct, `list(set(A)^set(B))` doesn't do the right thing when `B` contains elements not found in `A`; the result will *add* those `B`-only elements. You wanted `set(A) - set(B)`, not `set(A) ^ set(B)`. – ShadowRanger Sep 06 '19 at 10:24
  • It is about removing duplicate values and this does that perfectly! – Shamsheer Sep 06 '19 at 11:22