How to check whether a tuple exists in a Python list?


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


how to achieve this then?


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)


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 <strong>set</strong> though, because it has a much faster containment check.


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]]

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


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


This code works fine for me:

>>> a = [[1, 2], [3, 4], [13, 11]] >>> >>> for i in range(10): ... for j in range(10): ... if [i, j] in a: ... print [i, j] ... [1, 2] [3, 4] >>>

I'm not sure what is wrong with your code. For sure you have missing ']' in first line.


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


  • Compare Date & Time Javascript
  • Python how to create a dict of dict of list with defaultdict
  • How do I remove duplicates from an AutoHotkey array?
  • R matching more than 2 conditions and return the response value
  • Get uniq random lines from file and write them to another file using php
  • Disadvantages to high make job values
  • Variant of defaultdict for assigning value only once
  • Why fgetc too slow?
  • twisted.internet.error.ConnectError when run scrapy spider
  • How to initialize context? [closed]
  • Granting permissions to Azure Active Directory Web Application automatically
  • In matplotlib, how do you change the fontsize of a single figure?
  • Django model inheritance, filtering models
  • Entity Framework Code First TPC Inheritance Self-Referencing Child Class
  • How can I speed up CURL tasks?
  • Parse a date string in a specific locale (not timezone!)
  • Atlas images wrong size on iPad iOS 9
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Is possible to count alias result on mysql
  • TFS: Get latest causes slow project reloading
  • Validaiting emails with Net.Mail MailAddress
  • How to handle AllServersUnavailable Exception
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • ActionScript 2 vs ActionScript 3 performance
  • R: gsub and capture
  • 0x202A in filename: Why?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • reshape alternating columns in less time and using less memory
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?