189

I have a list 'a'

a= [(1,2),(1,4),(3,5),(5,7)]

I need to find all the tuples for a particular number. say for 1 it will be

result = [(1,2),(1,4)]

How do I do that?

Wolf
  • 9,679
  • 7
  • 62
  • 108
Bruce
  • 33,927
  • 76
  • 174
  • 262

10 Answers10

309

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]
Wolf
  • 9,679
  • 7
  • 62
  • 108
Nadia Alramli
  • 111,714
  • 37
  • 173
  • 152
170

There is actually a clever way to do this that is useful for any list of tuples where the size of each tuple is 2: you can convert your list into a single dictionary.

For example,

test = [("hi", 1), ("there", 2)]
test = dict(test)
print test["hi"] # prints 1
Jonathan
  • 123
  • 2
  • 9
Steven Holtzen
  • 1,736
  • 1
  • 11
  • 4
  • 17
    How do you apply this to Bruce's problem? – HelloGoodbye Jan 29 '14 at 08:10
  • 7
    Good answer (though possibly not for this question). Worked well for me to determine if a value was in a list of choice tuples (eg; if "hi" in test) – MagicLAMP Nov 04 '15 at 00:10
  • 14
    It doesn't really answer the question, as MagicLAMP suggests. Specifically, `dict(X)` converts X into a dictionary where the last tuple of any common first element, is the value that is used. In the example of the OP, this would return (1,4) as opposed to both (1,2) and (1,4). – BBischof Jun 12 '16 at 00:51
  • This may not answer the question above, but this is exactly what I'm looking for. – justin Jan 29 '21 at 05:16
  • Love it, thank you for sharing. – Sun Bee Dec 16 '21 at 01:21
  • Probably the most versatile for people looking for this list of tuples thing, love it! Thank you. – Czeskleba Jan 06 '22 at 09:29
27

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 ) )
Community
  • 1
  • 1
S.Lott
  • 384,516
  • 81
  • 508
  • 779
12
[tup for tup in a if tup[0] == 1]
Tendayi Mawushe
  • 25,562
  • 6
  • 51
  • 57
10
for item in a:
   if 1 in item:
       print item
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
10

The filter function can also provide an interesting solution:

result = list(filter(lambda x: x.count(1) > 0, a))

which searches the tuples in the list a for any occurrences of 1. If the search is limited to the first element, the solution can be modified into:

result = list(filter(lambda x: x[0] == 1, a))
MxNx
  • 1,342
  • 17
  • 28
3

Or takewhile, ( addition to this, example of more values is shown ):

>>> a= [(1,2),(1,4),(3,5),(5,7),(0,2)]
>>> import itertools
>>> list(itertools.takewhile(lambda x: x[0]==1,a))
[(1, 2), (1, 4)]
>>> 

if unsorted, like:

>>> a= [(1,2),(3,5),(1,4),(5,7)]
>>> import itertools
>>> list(itertools.takewhile(lambda x: x[0]==1,sorted(a,key=lambda x: x[0]==1)))
[(1, 2), (1, 4)]
>>> 
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
2

Using filter function:

>>> def get_values(iterables, key_to_find):
return list(filter(lambda x:key_to_find in x, iterables)) >>> a = [(1,2),(1,4),(3,5),(5,7)] >>> get_values(a, 1) >>> [(1, 2), (1, 4)]
pradam
  • 21
  • 2
2
>>> [i for i in a if 1 in i]

[(1, 2), (1, 4)]

suyash
  • 327
  • 2
  • 8
  • 1
    While correct, how is this different from `[item for item in a if 1 in item]` in the accepted answer posted 8 years earlier? Also note this will also match `(2, 1)` and `(4, 1)`. – Arjan Sep 14 '20 at 15:36
-3

if you want to search tuple for any number which is present in tuple then you can use

a= [(1,2),(1,4),(3,5),(5,7)]
i=1
result=[]
for j in a:
    if i in j:
        result.append(j)

print(result)

You can also use if i==j[0] or i==j[index] if you want to search a number in particular index

vimuth
  • 5,064
  • 33
  • 79
  • 116