Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to find tuple indices from other tuple list
Finding tuple indices from another tuple list is a common task in Python data manipulation. We can use several approaches: dictionary lookup with enumerate(), list comprehension, and the index() method.
An ordered and immutable group of objects is referred to as a tuple. Sequences are just what tuples and lists both are. Tuples and lists vary in that tuples cannot be altered, although lists may, and because tuples use parentheses while lists use square brackets.
Problem Statement
Given an input list containing tuples and another search tuple list, we need to find the indices of the search tuples in the input list ?
Input
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
print("Input List:", inputList)
print("Search List:", searchTupleList)
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Search List: [(1, 3), (5, 7), (3, 8), (7, 2)]
Using Dictionary Lookup with enumerate()
The enumerate() method adds a counter to an iterable and returns the enumerate object. We create a lookup dictionary for faster searches ?
Syntax
enumerate(iterable, start=0)
Parameters
iterable It can be any sequence/object/iterable supporting iteration
start enumerate() begins counting from this value. If the start is not specified, the value 0 is used.
Example
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
print("Input List:", inputList)
# input search tuple list whose values are to be searched in input list
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
# getting the element and index values of input list as value-key pairs
searchDict = {v: k for k, v in enumerate(inputList)}
# checking whether the tuple exists and if exists then storing the index
resultantList = [searchDict[idx] for idx in searchTupleList if idx in searchDict]
print("Index of searched tuples in input list:", resultantList)
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuples in input list: [0, 2]
Using List Comprehension with enumerate()
List comprehension provides a shorter syntax when you wish to build a new list based on the values of an existing list ?
Example
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
print("Input List:", inputList)
# input search tuple list whose values are to be searched in input list
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
# getting the result using list comprehension with enumerate
resultantList = [i for i, value in enumerate(inputList) for element in searchTupleList if element == value]
print("Index of searched tuples in input list:", resultantList)
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuples in input list: [0, 2]
Using the index() Method
The index() method returns the position of the first occurrence of the specified value ?
Syntax
list.index(element)
Example
# input list of tuples
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)]
print("Input List:", inputList)
# input search tuple list whose values are to be searched in the input list
searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
# creating a variable to store the result indices list
resultantList = []
# traversing through searchTupleList
for p in searchTupleList:
# checking whether the current element is present in input list
if p in inputList:
# getting the index of current element and appending it to the resultant list
resultantList.append(inputList.index(p))
print("Index of searched tuples in input list:", resultantList)
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuples in input list: [0, 2]
Performance Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Dictionary Lookup | O(n + m) | Large datasets, multiple searches |
| List Comprehension | O(n × m) | Concise readable code |
| index() Method | O(n × m) | Simple implementation |
Conclusion
Use dictionary lookup with enumerate() for better performance with large datasets. List comprehension offers concise syntax, while the index() method provides a straightforward approach for smaller lists.
