1

I have two lists of A and B. When I found a value of B in A, I want to remove the value in A. But without "redo" in python, it makes a problem. There might be diverse codings for that but I want to know a very smart or simple or pythonic coding.

I need "redo" for this, but python does not have "redo".

A = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
B = ['Feb', 'Mar', 'Apr']
for mon in A:
    if mon in B:
        A.remove(mon)
print(A)

returns ['Jan', 'Mar', 'May', 'Jun']

After removing 'Feb' in A, due to the A.remove() method, 'Mar' is skipped.

I need to explain my case more because there are many kinds of solutions. Basically I want to keep A because A is a reference with long list elements. Then during a looping of many B's, I want to remove those elements in B from A. So I need to keep A until the end of program, removing elements which belongs to any B.

Joonho Park
  • 511
  • 5
  • 17

4 Answers4

3

You can use a list comprehension:

[item for item in A if item not in B]
Carsten
  • 2,765
  • 1
  • 13
  • 28
  • I would agree with this answer but to just add it is bad to add or remove from a list that you are iterating over, it never ends well and I would think that this is the reason why you are having issues. – John Dowling Jul 16 '19 at 08:06
  • This is an answer. But following the extended (edited) my question, it might be a troublesome to make a new list rather than keeping A. In my opinion, Sukrit' solution [in duplicate page((https://stackoverflow.com/questions/18194968/python-remove-duplicates-from-2-lists))] looks best , which changes just A into a copy of A[:]. I am not sure there is any weak point there. – Joonho Park Jul 17 '19 at 09:03
  • If you don't want to set up a new list, you might want to use `A = [item for item in A if item not in B]` – Carsten Jul 17 '19 at 10:56
2

iterate over items in list B:

A = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
B = ['Feb', 'Mar', 'Apr']

for item in B:
    if item in A:
        A.remove(item)

print (A)

output:

['Jan', 'May', 'Jun']

NOTE:

whats happening in your code, you are iterating over items in a list from which you are removing items. the for loop continues its iteration from the index on which it stays. but if you remove item from the list,you have moved your items for idx -1, because of that its "skiping" some items from your list A

Example:

1.iter: 'Jan' (index 0) its not in the list B

2.iter: 'Feb' (index 1) its in the list B, remove 'Feb' from list A, after removing item, at index 1 now you will have 'Mar', but index where iteration is continuing is 2, because of that its "skipping" 'Mar'

ncica
  • 7,015
  • 1
  • 15
  • 37
1

Use set.difference

Ex:

A = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
B = ['Feb', 'Mar', 'Apr']

print(list(set(A).difference(set(B))))

Output:

['Jan', 'May', 'Jun']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

A generalization of this question across multiple lists to get a list of common elements:

l = [['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'], ['Feb', 'Mar', 'Apr'], ['Feb', 'Mar', 'Apr', 'Dec']]

list(set.intersection(*map(set, l)))

Output:

['Feb', 'Apr', 'Mar']
Stoner
  • 846
  • 1
  • 10
  • 30