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
CodePudding user response:
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]
>>>
CodePudding user response:
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
CodePudding user response:
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)
CodePudding user response:
Just use the symmetric difference operator with sets:
filtered_list = list(set(A) ^ set(B))
CodePudding user response:
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]