470,811 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Python "member of" function

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!

Apr 29 '06 #1
3 1920
br******@gmail.com wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
No man quite believes in any other man.
-- H.L. Mencken
Apr 29 '06 #2
br******@gmail.com:
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.

1 in [1,3,4] True 2 in [1,3,4]

False

--
René Pijlman
Apr 29 '06 #3
Thanks, I was using "in" incorrectly. Thanks a lot!

Apr 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gregory Bond | last post: by
3 posts views Thread by ruud.bos | last post: by
10 posts views Thread by Thorsten Ottosen | last post: by
2 posts views Thread by Lionel B | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.