Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Records with Value at K index

Improve Article
Save Article
  • Last Updated : 31 Jan, 2023
Improve Article
Save Article

Sometimes, while working with records, we might have a problem in which we need to find all the tuples of elements for a particular value at a particular Kth position of tuple. This seems to be a peculiar problem but while working with many keys in records, we encounter this problem. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop This is the brute force method by which this problem can be solved. In this we keep a check and append to list if we find specific record at Kth position in tuple. 

Python3




# Python3 code to demonstrate working of
# Records with Value at K index
# Using loop
 
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize ele
ele = 3
 
# initialize K
K = 1
 
# Records with Value at K index
# Using loop
# using y for K = 1
res = []
for x, y, z in test_list:
    if y == ele:
        res.append((x, y, z))
 
# printing result
print("The tuples of element at Kth position : " + str(res))

Output : 

The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
The tuples of element at Kth position : [(1, 3, 6), (6, 3, 0)]

  Method #2 : Using enumerate() + list comprehension The combination of above functions can be used to solve this problem. In this, we enumerate for the indices using enumerate(), rest is performed as in above method but in compact way. 

Python3




# Python3 code to demonstrate working of
# Records with Value at K index
# Using enumerate() + list comprehension
 
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize ele
ele = 3
 
# initialize K
K = 1
 
# Records with Value at K index
# Using enumerate() + list comprehension
res = [b for a, b in enumerate(test_list) if b[K] == ele]
 
# printing result
print("The tuples of element at Kth position : " + str(res))

Output : 

The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
The tuples of element at Kth position : [(1, 3, 6), (6, 3, 0)]

Method #3 : Using filter()
This is yet another approach to solve this problem. In this, we simply use filter() to check if the element at Kth position is equal to the element to be searched, if yes, then only we append to the list.

Python3




# Python3 code to demonstrate working of
# Records with Value at K index
# Using filter()
 
# initialize list
test_list = [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
   
# printing original list
print("The original list is : " + str(test_list))
   
# initialize ele
ele = 3
   
# initialize K
K = 1
   
# Records with Value at K index
# Using filter()
res = list(filter(lambda x : x[K] == ele, test_list))
   
# printing result
print("The tuples of element at Kth position : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output

The original list is : [(3, 1, 5), (1, 3, 6), (2, 5, 7), (5, 2, 8), (6, 3, 0)]
The tuples of element at Kth position : [(1, 3, 6), (6, 3, 0)]

Time complexity: o(n)

Auxiliary Space: o(n)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!