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:

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
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...