
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 First Element by Second in Tuple List in Python
The tuple list defines the two different datatypes of Python that represent the static and dynamic characteristics respectively. In a dictionary, we can create keys using tuples whereas lists cannot be used as keys. In Python, we have some built-in functions like next(), items(), index(), lambda, and, min() will be used to Find the first element by the second in the tuple list. This type of circumstance occurs when the second element holds the important information as a key for identifying the desired tuple.
Let's take an example of this:
The given tuple list,
my_tup = [("India", 35), ("Indonesia", 12), ("London", 31), ("Germany", 20)])
Searching the specific value that is present in a tuple of the list,
search_value = 31
Output
Germany
Explanation:
The variable named my_tup stores the tuple list by containing a string and an integer. As per the given problem statement, set the integer value 31 in the variable search_value. The final output becomes Germany.
Syntax
The following syntax is used in the examples-
next()
The next() is an in-built function that iterates the next item in an iterator.
items()
The items() is a built-in method in Python that returns the view object by containing a key with value pair.
index()
The index() is a built-in function in Python that can be used to search the element from the list and return the position of the element.
lambda
This lambda function in Python is known as an anonymous function. It can be used when function objects are required.
min()
The built-in function min returns the minimum item in iterable. This can also be used while finding the smallest element between two parameters.
Using a for loop
In the following example, the program uses for loop that iterates over the tuples. If the second element is found in the given input tuple list, it returns the corresponding first element. Otherwise, it returns none and display the result.
Example
def find_first_element(tuples, second_value): for first, second in tuples: if second == second_value: return first return None # Return None if the second value is not found # create the tuple list my_tuples = [("amar", 3), ("akbar", 2), ("anthony", 31)] second_value = 31 result = find_first_element(my_tuples, second_value) print(result)
Output
anthony
Using next()
In the following example, the program uses a recursive function which means it calls itself. Next, it will use the built-in function next() which iterates the next item according to the given input. The program also mentions the value of the second element which will be useful to find the first element of the tuple in the list.
Example
def find_first_element(tuples, second_value): result = next((first for first, second in tuples if second == second_value), None) return result # create the tuple list my_tuples = [("String", 3), ("letter", 2), ("word", 3)] second_value = 2 result = find_first_element(my_tuples, second_value) print(result)
Output
letter
Using dict() and next() function
In the following example, the program uses a built-in function dict() to convert the value of tuple list into dictionary that will be used for the comprehension technique to solve the first element by second in the tuple list and display the result.
Example
def find_first_element(tuples, second_value): dict_tuples = dict(tuples) result = next((key for key, value in dict_tuples.items() if value == second_value), None) return result # create the tuple list my_tuples = [("Manish", 3), ("Ram", 2), ("shyam", 3), ("Deepak", 4)] second_value = 3 result = find_first_element(my_tuples, second_value) print(result)
Output
Manish
Using a lambda function and the min() function
In the following example, the program uses the built-in function lambda to calculate the condition based on the first element by the second in tuple list.
Example
def find_first_element(tuples, second_value): result = min(tuples, key=lambda x: (x[1] != second_value, tuples.index(x)))[0] return result # create the tuple list my_tuples = [("pen", 1), ("pencil", 2), ("notebook", 3), ("eraser", 4)] second_value = 4 result = find_first_element(my_tuples, second_value) print(result)
Output
eraser
Conclusion
The program involves the iteration over the given tuple list which is compared by mentioning the second value of the tuple to return the first tuple and satisfies with specific conditions and operations. There are a few applications used in the real world such as Sorting and filtering, Data Processing, Data Analysis and Statistics etc.