python - if not in list

Boosted_d16 picture Boosted_d16 · Apr 3, 2014 · Viewed 148.2k times · Source

I have two lists:

mylist = ['total','age','gender','region','sex']
checklist = ['total','civic']

I have to work with some code I have inherited which looks like this:

for item in mylist:
    if item in checklist:
        do something:

How can I work with the code above to tell me that 'civic' is not in mylist?.

This would've been the ideal way to do it but I cant use it, don't ask me why.

for item in checklist:
    if item not in mylist:
        print item

Outcome:

civic

Answer

Will picture Will · Dec 8, 2014

Your code should work, but you can also try:

    if not item in mylist :