How can I search list(s) where all the elements exactly match what I’m looking for. For instance, I want to verify if the following list consist of ‘a’, ‘b’ and ‘c’, and nothing more or less.
lst=['a', 'b', 'c']
I did this:
if 'a' in lst and 'b' in lst and 'c' in lst:
#do something
Many thanks in advance.
You can sort the lists and then simply compare them:
sorted( list_one ) == sorted( list_two ).Also you can convert both lists to sets and compare them. But be careful, sets eat duplicate items!
set( list_one ) == set( list_two ).Sets can also tell you which items either set is lacking:
set(list_one) ^ set(list_two).Some examples: