How to check if a list contains an item

E

Esmail

python doesn't have a list.contains() method?

How about the 'in' operator?

In [1]: l1=['aa', 'bb', 'cc']

In [2]: 'aa' in l1
Out[2]: True

In [3]: 'ab' in l1
Out[3]: False

In [4]: 'cc' in l1
Out[4]: True

Will this help?

Esmail
 
M

MRAB

python doesn't have a list.contains() method?
It does have a __contains__ method, but that's not called directly. Use
"item in my_list" instead (and the opposite is "item not in my_list").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,502
Members
48,189
Latest member
StaciLgf76

Latest Threads

Top