Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - if not in list [duplicate]

Tags:

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 
like image 269
Boosted_d16 Avatar asked Apr 03 '14 09:04

Boosted_d16


People also ask

How do you know if a element is repeated in list?

To check if a list has only unique elements, we can also count the occurrence of the different elements in the list. For this, we will use the count() method. The count() method, when invoked on a list, takes the element as input argument and returns the number of times the element is present in the list.

Can list have duplicates?

Python list can contain duplicate elements.


1 Answers

Your code should work, but you can also try:

    if not item in mylist : 
like image 140
Will Avatar answered Nov 09 '22 15:11

Will