Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find an Exact Tuple Match in a List of Tuples and Return Its Index [duplicate]

I am trying to figure out how to determine if a tuple has an exact match in a list of tuples, and if so, return the index of the matching tuple. For instance if I have:

TupList = [('ABC D','235'),('EFG H','462')]

I would like to be able to take any tuple ('XXXX','YYYY') and see if it has an exact match in TupList and if so, what its index is. So for example, if the tuple ('XXXX','YYYY') = (u'EFG H',u'462') exactly, then the code will return 1.

I also don't want to allow tuples like ('EFG', '462') (basically any substring of either tuple element) to match.

like image 296
Mark Clements Avatar asked Nov 27 '13 10:11

Mark Clements


People also ask

How do you find duplicates in tuples?

Initial approach that can be applied is that we can iterate on each tuple and check it's count in list using count() , if greater than one, we can add to list. To remove multiple additions, we can convert the result to set using set() .

Can list and tuple have duplicate values?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.


2 Answers

Use list.index:

>>> TupList = [('ABC D','235'),('EFG H','462')]
>>> TupList.index((u'EFG H',u'462'))
1
like image 167
Ashwini Chaudhary Avatar answered Sep 18 '22 18:09

Ashwini Chaudhary


I think you can do it by this

TupList = [('ABC D','235'),('EFG H','462')]
if ('ABC D','235') in TupList:
   print TupList.index(i)
like image 40
Mero Avatar answered Sep 18 '22 18:09

Mero