So heres my code:
item = [0,1,2,3,4,5,6,7,8,9]
z = [] # list of integers
for item in z:
if item not in z:
print item
z contains a list of integers. I want to compare item to z and print out the numbers that are not in z when compared to item.
I can print the elements that are in z when compared not item, but when I try and do the opposite using the code above nothing prints.
Any help?
Your code is not doing what I think you think it is doing. The line
for item in z:will iterate throughz, each time makingitemequal to one single element ofz. The originalitemlist is therefore overwritten before you’ve done anything with it.I think you want something like this:
But you could easily do this like:
or (if you don’t mind losing duplicates of non-unique elements):