Python – Retain K match index values from other list

Sometimes, while working with Python lists, we can have a problem in which we need to retain only the strings with match a particular value from the corresponding list at same index. This can have application in many domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + zip()
The combination of above functionalities can be used to perform this task. In this, we extract the list after selective zipping of both the list matching K.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Retain K match index values from other list
# using zip() + list comprehension
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'best', 'for', 'Geeks', 'and', 'CS']
test_list2 = [4, 1, 4, 3, 4, 2, 4]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Initializing K 
K = 4
  
# Group elements from Dual List Matrix
# using zip() + list comprehension
res = [x for x, y in zip(test_list1, test_list2) if y == K]
              
# printing result 
print ("The filtered list : " + str(res))

chevron_right


Output :

The original list 1 is : [‘Gfg’, ‘is’, ‘best’, ‘for’, ‘Geeks’, ‘and’, ‘CS’]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The filtered list : [‘Gfg’, ‘best’, ‘Geeks’, ‘CS’]

Method #2 : Using compress() + list comprehension
This is yet another way in which this task can be performed. In this, we use compress() instead of zip() to solve the problem.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Retain K match index values from other list
# using compress + list comprehension
from itertools import compress
  
# Initializing lists
test_list1 = ['Gfg', 'is', 'best', 'for', 'Geeks', 'and', 'CS']
test_list2 = [4, 1, 4, 3, 4, 2, 4]
  
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
  
# Initializing K 
K = 4
  
# Group elements from Dual List Matrix
# using compress + list comprehension
res = list(compress(test_list1, map(lambda ele: ele == K, test_list2)))
              
# printing result 
print ("The filtered list : " + str(res))

chevron_right


Output :

The original list 1 is : [‘Gfg’, ‘is’, ‘best’, ‘for’, ‘Geeks’, ‘and’, ‘CS’]
The original list 2 is : [4, 1, 4, 3, 4, 2, 4]
The filtered list : [‘Gfg’, ‘best’, ‘Geeks’, ‘CS’]




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.