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
test_list = [( 4 , 5 ), ( 5 , 6 ), ( 1 , 3 ), ( 6 , 9 )]
print ( "The original list is : " + str (test_list))
K = 6
res = [x for (x, y) in test_list if y = = K]
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
test_list = [( 4 , 5 ), ( 5 , 6 ), ( 1 , 3 ), ( 6 , 9 )]
print ( "The original list is : " + str (test_list))
K = 6
res = next ((x for x, y in test_list if y = = K), None )
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
test_list = [( 4 , 5 ), ( 5 , 6 ), ( 1 , 3 ), ( 6 , 9 )]
print ( "The original list is : " + str (test_list))
K = 6
res = []
for tup in test_list:
if tup[ 1 ] = = K:
res.append(tup[ 0 ])
break
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]
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...