Examples of how to check if an element is in a list or not in python:
Lets create for instance the following list:
['a', 'b', 'c', 'd', 'e', 'f']
To check if the element 'c' is in the list called here l, a solution is to use the following logical expression:
>>> 'c' in l
check now if 'g' is in the list
>>> 'g' in l
Can then be used with if, illustration:
>>> if 'c' in l: print('do something !')
do something !
To check if an element is not in a list, a solution is to use the expression 'not in', example:
>>> 'h' not in l
>>> l = ['Coucou','Salut','Bonjour','He']
>>> 'Salut' in l
>>> 'Hello' in l
>>> l = [3,1,7,5]
>>> 7 in l
>>> 2 in l
Hi, I am Ben.
I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!