Open In App

Python – Find first element by second in tuple List

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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
Previous
Next
Share your thoughts in the comments

Similar Reads