Python – Extract Preceding Record from list values

Sometimes, while working with Tuple records, we can have a problem in which we need to extract the record, which is preceeding to particular key provided. This kind of problem can have application in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(‘Gfg’, 3), (‘is’, 4), (‘best’, 1), (‘for’, 10), (‘geeks’, 11)], key = ‘geeks’
Output : (‘for’, 10)

Input : test_list = [(‘Gfg’, 3), (‘is’, 4), (‘best’, 1), (‘for’, 10), (‘geeks’, 11)], key = ‘is’
Output : (‘Gfg’, 3)

Method #1 : Using zip() + enumerate() + loop
The combination of above functions can be used to perform this particular task. In this, we create 2 lists, one with starting with next index. The zip(), helps to connect them and return the desired result using enumerate() for extracting index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Preceeding Record
# Using zip() + enumerate() + loop
  
# initializing list
test_list = [('Gfg', 3), ('is', 4), ('best', 1), ('for', 10), ('geeks', 11)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Key
key = 'for'
  
# Extract Preceding Record
# Using zip() + enumerate() + loop
for idx, (a, b) in enumerate(zip(test_list, test_list[1:])):
    if b[0] == key:
        res = (a[0], a[1])
  
# printing result 
print("The Preceeding record : " + str(res)) 

chevron_right


Output :



The original list is : [('Gfg', 3), ('is', 4), ('best', 1), ('for', 10), ('geeks', 11)]
The Preceeding record : ('best', 1)

 

Method #2 : Using list comprehension + enumerate()
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting the previous element to manually test previous key, rather than creating a separate list as in previous method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Preceeding Record
# Using list comprehension + enumerate()
  
# initializing list
test_list = [('Gfg', 3), ('is', 4), ('best', 1), ('for', 10), ('geeks', 11)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Key
key = 'for'
  
# Extract Preceding Record
# Using list comprehension + enumerate()
res = [(test_list[idx - 1][0], test_list[idx - 1][1]) 
      for idx, (x, y) in enumerate(test_list) if x == key and idx > 0]
  
# printing result 
print("The Preceeding record : " + str(res)) 

chevron_right


Output :

The original list is : [('Gfg', 3), ('is', 4), ('best', 1), ('for', 10), ('geeks', 11)]
The Preceeding record : [('best', 1)]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.