How to check whether a tuple exists in a Python list?
Asked Answered
P

5

15

I am new to Python, and I am trying to check whether a pair [a,b] exists in a list l=[[a,b],[c,d],[d,e]]. I searched many questions, but couldn't find precise solution. Please can someone tell me the right and shortest way of doing it?

when i run :

a=[['1','2'],['1','3']]
for i in range(3):
    for j in range(3):
        if [i,j] in a:
            print a

OUTPUT IS BLANK

how to achieve this then?

Proceeding answered 11/3, 2012 at 11:29 Comment(4)
Your code works for me, output is [[1,2], [1,3]] as expected.Marvelofperu
Ah, after you edited the code it's quite obvious, [1,2] != ['1','2'].Marvelofperu
sorry, i wanted this, i did'nt write properlyProceeding
@Marvelofperu can you tell me how to do it now..?Proceeding
M
10

The code does not work because '1' != 1 and, consequently, ['1','2'] != [1,2] If you want it to work, try:

a=[['1','2'],['1','3']]
for i in range(3):
    for j in range(3):
        if [str(i), str(j)] in a: # Note str
            print a

(But using in or sets as already mentioned is better)

Marvelofperu answered 11/3, 2012 at 11:50 Comment(0)
V
28

Here is an example:

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

If you need to do this a lot of times consider using a set though, because it has a much faster containment check.

Verve answered 11/3, 2012 at 11:31 Comment(1)
i am using nested loops for i in range(3): and for j in range(3) and then trying for [i,j] in a....but its not identifyingProceeding
M
10

The code does not work because '1' != 1 and, consequently, ['1','2'] != [1,2] If you want it to work, try:

a=[['1','2'],['1','3']]
for i in range(3):
    for j in range(3):
        if [str(i), str(j)] in a: # Note str
            print a

(But using in or sets as already mentioned is better)

Marvelofperu answered 11/3, 2012 at 11:50 Comment(0)
T
0
a=[['1','2'],['1','3']]
[[x,y] for x, y in a if [x,y]  == ['1','3']]

As per my understanding of you question, you were trying to find a specific list within another list.

for example ['1', '3'] within a. using list comprehension you can solve this.

Tybie answered 23/7, 2023 at 5:56 Comment(1)
Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Engdahl
M
-2

In my interpreter (IPython 0.10, Python 2.7.2+) your code gives the correct output:

In [4]: a=[[1,2],[1,3]]

In [5]: for i in range(3):
   ...:         for j in range(3):
   ...:             if [i,j] in a:
   ...:                 print a
   ...: 
[[1, 2], [1, 3]]

(This should be a comment, but I can't leave them yet.)

EDIT:

Turns out you had strings in the a list. Then you need to convert your ints to str as well:

a=[['1','2'],['1','3']]
for i in range(3):
    for j in range(3):
        if [str(i), str(j)] in a:
            print a
Misconduct answered 11/3, 2012 at 11:45 Comment(0)
B
-2

Don't forget that [a, b] is not [b, a] in python so you might want to order the 2 values in your tuples if you want to consider [A, B] and [B, A] is the same:

You might also want to use set(your_list) if your list is big and it has redundancy.

In your code example you are compaing integers and strings :

['1', '2'] # this is a 2-list of strings '1' and '2'
[1, 2] # this is a 2-list of integers 1 and 2
Burlburlap answered 11/3, 2012 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.