Sponsored By

Sign in to follow this  
shanenin

Testing For A Number (python)

Recommended Posts

lets say I have a list called lis

lis = [(1, 2), (3, 4), (5, 6), (7, 8)]

lets say I want to test for a numner in the first position of each tuple. to give you an idea of what I want to do, I will show you code that does not work, but might make my point

if 3 in lis[0-3][0]:

the code would read if 3 is in the first position of the first four tuples:

Share this post


Link to post
Share on other sites

Hmm. My inclination would be to use map

if 3 in map(lambda(t): t[0], lis):

, but kind of thing is discouraged in Python these days, or a list comprehension

if [i for i in lis if i[0] == 3]:

but neither is very efficient.

Edited by jcl

Share this post


Link to post
Share on other sites

thanks. I like the list comprehension. That is sure to come in handy again.

I did it this way

if 3 in [ i[0] for i in lis ]:

Edited by shanenin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this