Find an element in a list of tuples
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 Question
00:25 Accepted answer (Score 307)
00:46 Answer 2 (Score 167)
01:07 Answer 3 (Score 27)
01:30 Answer 4 (Score 12)
01:39 Thank you
--
Full question
https://stackoverflow.com/questions/2191...
Answer 2 links:
[List Comprehensions]: http://docs.python.org/tutorial/datastru...
[generator functions]: http://www.python.org/dev/peps/pep-0255/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #search #tuples
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over
--
Chapters
00:00 Question
00:25 Accepted answer (Score 307)
00:46 Answer 2 (Score 167)
01:07 Answer 3 (Score 27)
01:30 Answer 4 (Score 12)
01:39 Thank you
--
Full question
https://stackoverflow.com/questions/2191...
Answer 2 links:
[List Comprehensions]: http://docs.python.org/tutorial/datastru...
[generator functions]: http://www.python.org/dev/peps/pep-0255/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #list #search #tuples
#avk47
ACCEPTED ANSWER
Score 317
If you just want the first number to match you can do it like this:
[item for item in a if item[0] == 1]
If you are just searching for tuples with 1 in them:
[item for item in a if 1 in item]
ANSWER 2
Score 29
Read up on List Comprehensions
[ (x,y) for x, y in a if x == 1 ]
Also read up up generator functions and the yield statement.
def filter_value( someList, value ):
for x, y in someList:
if x == value :
yield x,y
result= list( filter_value( a, 1 ) )
ANSWER 3
Score 12
[tup for tup in a if tup[0] == 1]
ANSWER 4
Score 10
for item in a:
if 1 in item:
print item