0
A = [int(x) for x in input('Enter your elements: ').split()]
B = [int(y) for y in input('Enter your elements: ').split()]

for i in A:
    if i in B:
        A.remove(i)
print(A)

#Here I will give 4 1 3 2 in Array A

#And In Array B I will give 3 1

#Hence there is 3 1 common between 2 arrays

#I want to return 4 2 as output
Chris
  • 26,361
  • 5
  • 21
  • 42
EE 217
  • 11
  • Small note: those are lists rather than arrays. – Chris Sep 07 '22 at 16:09
  • As well as explaining what the desired result is, please always explain what the actual result is or what errors you are seeing. And generally speaking, mutating a container of items as you're iterating over that container can be problematic. – jarmod Sep 07 '22 at 16:10
  • Simple one-liner is: `C = [a for a in A if a not in B]` – jarmod Sep 07 '22 at 16:16
  • 2
    Removing elements from a list while iterating over may produce unexpected results – DarkKnight Sep 07 '22 at 16:31
  • Does this answer your question? [Remove all the elements that occur in one list from another](https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another) – OrenIshShalom Sep 07 '22 at 16:52

5 Answers5

2

Let's try this:

>>> A = [int(x) for x in input('Enter your elements: ').split()]
Enter your elements: 5 6 7 8 9
>>> B = [int(y) for y in input('Enter your elements: ').split()]
Enter your elements: 1 2 3 4 5 6
>>> for i in A:
...     if i in B:
...         A.remove(i)
...
>>> A
[6, 7, 8, 9]
>>>

Now, you would expect A to be [7, 8, 9], but modifying a list while iterating over it is a really bad idea.

We can see what's going on with some simple debugging with print.

>>> a = [5, 6, 7, 8, 9]
>>> b = [1, 2, 3, 4, 5, 6]
>>> for x in a:
...   print(x)
...   if x in b:
...     a.remove(x)
...
5
7
8
9
>>> a
[6, 7, 8, 9]
>>>

Because 5 is removed from A, 6 is now the first element in A but we've moved past that, so the next element evaluated is 7, and 6 never has a chance to be removed from A.

Rather, let's generate a new list with the difference.

>>> A = [int(x) for x in input('Enter your elements: ').split()]
Enter your elements: 5 6 7 8 9
>>> B = [int(y) for y in input('Enter your elements: ').split()]
Enter your elements: 1 2 3 4 5 6
>>> C = [x for x in A if not x in B]
>>> C
[7, 8, 9]
>>>

That looks more like what you expected.

Note that A and B are unmodified. Also note that generally variables begin with lowercase letters in Python.

Should you wish to modify one of the lists, you can simply assign the new list back to it.

>>> a = [5, 6, 7, 8, 9]
>>> b = [1, 2, 3, 4, 5, 6]
>>> a = [x for x in a if not x in b]
>>> a
[7, 8, 9]
>>>
Chris
  • 26,361
  • 5
  • 21
  • 42
1

Sets are the most pythonic way to handle "Find me items that are in this list/not in this other list", if the elements are guaranteed to be unique.

shared = set(A).intersection(set(B))
return_list = list(set(A)-shared)
return return_list
David_Leber
  • 146
  • 6
  • 2
    note, you can just do `set(A) - set(B)` (or `set(A).difference(B)`) you don't have to find the intersection first. Also note, if you use the named methods, you don't have to convert to `set` first – juanpa.arrivillaga Sep 07 '22 at 16:12
  • Using sets may work except that there could be a reliance on the order of items in the list in which case, depending the list contents, that might change. There's also a problem if list A contains duplicate values that are not in list B – DarkKnight Sep 07 '22 at 16:29
1

You can use set functionality to determine the intersection then remove elements as appropriate.

This technique ensure that duplicate values in list A that are not in list B will be retained.

The removal is carried out in situ.

Order of list A is maintained.

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

for i in set(A).intersection(B):
    while i in A:
        A.remove(i)

print(A)

Output:

[10, 11, 11, 1, 3]

Here's an alternative without using sets:

from collections import Counter

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

counter = Counter(A)

for e in B:
    for _ in range(counter.get(e, 0)):
        A.remove(e)

print(A)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0

Just use the symmetric difference operator with sets:

filtered_list = list(set(A) ^ set(B))
0

Just use list comprehension and assign to A:

A = [int(x) for x in input('Enter your elements: ').split()]
B = [int(y) for y in input('Enter your elements: ').split()]
A = [i for i in A if i not in B]
print(A)

# Enter your elements: 4 1 3 2
# Enter your elements: 3 1
# [4, 2]

But if you really need to modify original A, you can use this:

A[:] = [i for i in A if i not in B]
Arifa Chan
  • 947
  • 2
  • 6
  • 23