Python - Find first element by second in tuple List
Last Updated :
17 May, 2023
Sometimes, while working with Python records, we can have a problem in which we need to find the first element of tuple from the given second element. This kind of problem can occur in domains such as web development. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(4, 5), (5, 6), (1, 3), (6, 6)] K = 6 Output : [5, 6] Input : test_list = [(4, 5), (5, 7), (1, 3), (6, 8)] K = 6 Output : []
Method #1 : Using list comprehension This is one of the ways in which this task can be performed. In this, we iterate for each tuple, and if we find key matching to value, we store in result list.
Python3
# Python3 code to demonstrate working of
# Find first element by second in tuple List
# Using list comprehension
# initializing list
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# Find first element by second in tuple List
# Using list comprehension
res = [x for (x, y) in test_list if y == K]
# printing result
print("The key from value : " + str(res))
Output : The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The key from value : [5]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using next() + generator expression This is yet another way in which this task can be solved. In here, the next() is used to get the successive elements and generator expression is used to check for the logic.
Python3
# Python3 code to demonstrate working of
# Find first element by second in tuple List
# Using next() + generator expression
# initializing list
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# Find first element by second in tuple List
# Using next() + generator expression
res = next((x for x, y in test_list if y == K), None)
# printing result
print("The key from value : " + str(res))
Output : The original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The key from value : 5
Time Complexity: O(n) where n is the number of elements in the in the list “test_list”. The next() + generator expression is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) as only constant additional space is required.
Method #3 : Using loop
This is yet another way in which this task can be solved. In here, we use a loop to iterate through the list of tuples and check if each tuple's second element matches the given value k. If a matching tuple is found, we can append its first element to a list.
Algorithm
1. Initialize an empty list to store the first element of the tuple whose second element equals to K.
2. Loop over each tuple tup in test_list:
a. If tup[1] is equal to K:
i. Append tup[0] to the res list.
ii. Break out of the loop.
3. Print the res list to display the first element whose second element equals to K.
Python3
# Python3 code to demonstrate working of
# Find first element by second in tuple List
# Using loop
# initializing list
test_list = [(4, 5), (5, 6), (1, 3), (6, 9)]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 6
# loop to find first element by second in tuple List
res = []
for tup in test_list:
if tup[1] == K:
res.append(tup[0])
break
# printing result
print("The key from value : " + str(res))
OutputThe original list is : [(4, 5), (5, 6), (1, 3), (6, 9)]
The key from value : [5]
Complexity
Time complexity O(n), where n is the length of the input list test_list.
Auxiliary Space O(n), where n is the length of the input list test_list
Similar Reads
How toGet First and Last Elements from a Tuple in Python Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we'll explore how to achieve this using the various methods in Python. Get First and Last Elements from a Tuple Using Index
2 min read
Python - Accessing nth element from tuples in list We are given tuples in list we need to access Nth elements from tuples. For example, we are having a list l = [(1, 2), (3, 4), (5, 6)] we need to access the Nth element from tuple suppose we need to access n=1 or 1st element from every tuple present inside list so that in this case the output should
2 min read
Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
Find the Tuples Containing the Given Element from a List of Tuples - Python The task of finding tuples containing a given element from a list of tuples in Python involves searching for a specific target value within each tuple. For example, given a list like [(1, 2, 3), (4, 5, 6)] and a target 2, the goal is to extract tuples containing 2, resulting in [(1, 2, 3)].Using in
3 min read
Python | Linear search on list or tuples Let us see a basic linear search operation on Python lists and tuples. A simple approach is to do a linear search, that is Start from the leftmost element of the list and one by one compare x with each element of the list.If x matches with an element, return True.If x doesnât match with any of the e
2 min read