I have been up far too long tonight working on a long program. But I have hit a simple roadblock. Can any one tell me why this code is working the way it is?
I have two lists. I want list2 to only contain numbers that are not in list1.
logically this seems like it should work. But it doest at all. Why?
list1 = [1,2,3,4,5,6,7,8]
list2 = [12,15,16,7,34,23,5,23,76,89,9,45,4]
for ch in list2:
if ch in list1:
list2.remove(ch)
return list2
somehow, this returns:
[15, 7, 5, 23, 76, 9, 4]
Why?
and how can I accomplish what I need?
When you modify a sequence you are iterating over, it will yield unexpected results. I’d do it this way, which takes advantage of fast
setoperations.Whether this is faster or slower than using a list comprehension depends on the sizes of
list1andlist2, and whether you can make one into asetas part of initialization rather than multiple times in a loop.