
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Tuple Indices from Other Tuple List in Python
To find the tuple indices from other tuple list we can use enumerate function and index function of Python.
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.
Example
Assume we have taken an input list containing tules and another search tuple list. We will now search the indices of the search tuple list in the given input list
Input
inputList = [(1, 3), (2, 4), (5, 7), (6, 8)] searchTupleList = [(1, 3), (5, 7), (3, 8), (7, 2)]
Output
Index of searched tuple in the input list: [0, 2]
Using list comprehension, lookup dictionary and enumerate() function
The enumerate() method adds a counter to an iterable and returns the enumerate object.
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.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input list of tuples.
Print the input list.
Create another variable to store the input search tuple list whose values are to be searched in the input list.
Use the enumerate() function to get the element and index values of the input list as value-key pairs.
Use the list comprehension to traverse through the searchTuple list and check whether the element exists in the above lookup dictionary.
If it exists then store its index of it.
Example
The following program returns tuple indices from other tuple list using list comprehension, lookup dictionary, and enumerate() function -
# input list of tuples inputList = [(1, 3), (2, 4), (5, 7), (6, 8)] # printing input list print("Input List: ", inputList) # input search tuple list whose values are to be searched in input list searchTupleList = [(10, 6), (5, 7), (3, 8), (7, 2)] # getting the element and index values of input list as a value-key pairs searchDict = {v: k for k, v in enumerate(inputList)} # Checking whether the tuple exits and if exists then storing the index of it. resultantList = [searchDict[idx] for idx in searchTupleList if idx in searchDict] # printing resultant list of index print("Index of searched tuple in input list:", resultantList)
Output
On executing, the above program will generate the following output -
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuple in input list: [2]
Using list comprehension and enumerate() function
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
Example
The following program returns tuple indices from other tuple list using list comprehension and enumerate() function -
# input list of tuples inputList = [(1, 3), (2, 4), (5, 7), (6, 8)] # printing input list 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 the concise syntax using list comprehension resultantList = [i for i, value in enumerate(inputList) for element in searchTupleList if element == value] # printing resultant list of indices print("Index of searched tuple in input list:", resultantList)
Output
On executing, the above program will generate the following output -
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuple in input list: [0, 2]
Using the index() method
The position at the first occurrence of the provided value is returned by the index() method.
Syntax
list.index(element)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input list of tuples.
Print the input list.
Create another variable to store the input search tuple list whose values are to be searched in the input list.
Initialize a new empty list to store the resultant list.
Use the for loop to traverse through the searchTupleList.
Use the if conditional statement to check whether the current element is present in the input list.
Use the index() function to get the index of the current element and append it to the resultant list using the append() function( adds the element to the list at the end).
Print the resultant list.
Example
The following program returns tuple indices from other tuple list using list comprehension and enumerate() function -
# input list of tuples inputList = [(1, 3), (2, 4), (5, 7), (6, 8)] # printing input list print("Input List: ", inputList) # input search tuple list whose values are to be searched in the input list searchTupleList = [(10, 6), (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 an input list if p in inputList: # getting the index of current element and appending it to the resultant list resultantList.append(inputList.index(p)) # printing resultant list of indices print("Index of searched tuple in input list:", resultantList)
Output
On executing, the above program will generate the following output -
Input List: [(1, 3), (2, 4), (5, 7), (6, 8)] Index of searched tuple in input list: [2]
Conclusion
In this article, we have learned how to find tuple indices from other tuple lists using 3 different methods. We also learned how to iterate through the iterable's index and element values using the enumerate() function. Finally, we learned how to use the index() function to retrieve the tuple pairs.