Python "member of" function

B

brianlum

Hi,

I was wondering if anyone knew of a built in Python function that will
check if an item is a member of a list, i.e., if item i is a member of
list l.

I read of a function "in" but I can't seem to get that to work and
finding pages for "python in" does not reveal very many relevant
sources.

Thanks!
 
E

Erik Max Francis

I was wondering if anyone knew of a built in Python function that will
check if an item is a member of a list, i.e., if item i is a member of
list l.

I read of a function "in" but I can't seem to get that to work and
finding pages for "python in" does not reveal very many relevant
sources.

Yes, `in` is the relevant operator. It's quite simple to use:
>>> a = [1, 4, 9, 16, 25]
>>> 2 in a False
>>> 4 in a
True

If you're having problems using it in some circumstance, you'll have to
be specific.
 

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

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top