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
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.
Python list can contain duplicate elements.
Your code should work, but you can also try:
if not item in mylist :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With