Use in
operator:
>>> lis = ['foo', 'boo', 'hoo']
>>> 'boo' in lis
True
>>> 'zoo' in lis
False
You can also use lis.index
which will return the index of the element.
>>> lis.index('boo')
1
If the element is not found, it will raise ValueError
:
>>> lis.index('zoo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'zoo' is not in list
UPDATE
As Nick T commented, if you don't care about order of items, you can use set
:
>>> lis = {'foo', 'boo', 'hoo'} # set literal == set(['foo', 'boo', 'hoo'])
>>> lis.add('foo') # duplicated item is not added.
>>> lis
{'boo', 'hoo', 'foo'}
set
, which, if you attempt to add a duplicate, it ignores it.int
,dict
,list
, andstr
.