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.
# 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)) |
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.
# 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)) |
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’]
Recommended Posts:
- Python - Retain list elements value items
- Python | Retain K consecutive elements
- Python | Retain records of specific length
- Python | Retain K Front and Rear elements
- Python - Retain records with N occurrences of K
- Python - Retain all K elements Rows
- Python - Retain Numbers in String
- Python - Retain first N Elements of a String and Replace the Remaining by K
- Python - Index match element Product
- Python | Remove all values from a list present in other list
- Python - Test rear digit match in all list elements
- Python - Match Kth number digit in list elements
- Python - Replace index elements with elements in Other List
- Python | Initializing dictionary with list index values
- Python | Initializing dictionary with list index-values
- Python - Values Frequency Index List
- Python - Assign values to Values List
- Regular Expressions in Python | Set 2 (Search, Match and Find All)
- Python | Get match indices
- Python | Pandas Series.str.match()
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.

