Skip to content
Related Articles

Related Articles

Python – Filter the List of String whose index in second List contains the given Substring

Improve Article
Save Article
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

Given two lists, extract all elements from the first list, whose corresponding index in the second list contains the required substring.

Examples:

Input : test_list1 = [“Gfg”, “is”, “not”, “best”, “and”, “not”, “CS”], 
test_list2 = [“Its ok”, “all ok”, “wrong”, “looks ok”, “ok”, “wrong”, “thats ok”], sub_str = “ok” 
Output : [‘Gfg’, ‘is’, ‘best’, ‘and’, ‘CS’] 
Explanation : All retained contain “ok” as substring in corresponding idx, e.g : Gfg -> Its ok ( has ok ) as substr.

Input : test_list1 = [“Gfg”, “not”, “best”], 
test_list2 = [“yes”, “noo”, “its yes”], sub_str = “yes” 
Output : [‘Gfg’, ‘best’] 
Explanation : All retained contain “yes” as substring in corresponding idx, e.g : Gfg -> yes ( has yes ) as substr. 

Method #1 : Using zip() + loop + in operator

In this, we combine the indices using zip(), and in operator is used to check for substring. Loop is used to the task of iteration.

Python3




# Python3 code to demonstrate working of
# Extract elements filtered by substring
# from other list Using zip() + loop + in
# operator
 
# initializing list
test_list1 = ["Gfg", "is", "not", "best", "and",
              "not", "for", "CS"]
test_list2 = ["Its ok", "all ok", "wrong", "looks ok",
              "ok", "wrong", "ok", "thats ok"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# initializing substr
sub_str = "ok"
 
res = []
# using zip() to map by index
for ele1, ele2 in zip(test_list1, test_list2):
 
    # checking for substring
    if sub_str in ele2:
        res.append(ele1)
 
# printing result
print("The extracted list : " + str(res))

Output

The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']
The original list 2 is : ['Its ok', 'all ok', 'wrong', 'looks ok', 'ok', 'wrong', 'ok', 'thats ok']
The extracted list : ['Gfg', 'is', 'best', 'and', 'for', 'CS']

Method #2 : Using list comprehension + zip() 

This is similar to above method. The only difference here is that list comprehension is used as shorthand to solve the problem.

Python3




# Python3 code to demonstrate working of
# Extract elements filtered by substring
# from other list Using list comprehension + zip()
 
# initializing list
test_list1 = ["Gfg", "is", "not", "best", "and",
              "not", "for", "CS"]
test_list2 = ["Its ok", "all ok", "no", "looks ok",
              "ok", "wrong", "ok", "thats ok"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# initializing substr
sub_str = "ok"
 
# using list comprehension to perform task
res = [ele1 for ele1, ele2 in zip(test_list1, test_list2) if sub_str in ele2]
 
# printing result
print("The extracted list : " + str(res))

Output

The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']
The original list 2 is : ['Its ok', 'all ok', 'no', 'looks ok', 'ok', 'wrong', 'ok', 'thats ok']
The extracted list : ['Gfg', 'is', 'best', 'and', 'for', 'CS']

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #3 : Using find() method

Python3




# Python3 code to demonstrate working of
# Extract elements filtered by substring
 
# initializing list
test_list1 = ["Gfg", "is", "not", "best", "and",
              "not", "for", "CS"]
test_list2 = ["Its ok", "all ok", "wrong", "looks ok",
                        "ok", "wrong", "ok", "thats ok"]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# initializing substr
sub_str = "ok"
 
res = []
for i in range(0, len(test_list2)):
    if test_list2[i].find(sub_str) != -1:
        res.append(test_list1[i])
 
 
# printing result
print("The extracted list : " + str(res))

Output

The original list 1 is : ['Gfg', 'is', 'not', 'best', 'and', 'not', 'for', 'CS']
The original list 2 is : ['Its ok', 'all ok', 'wrong', 'looks ok', 'ok', 'wrong', 'ok', 'thats ok']
The extracted list : ['Gfg', 'is', 'best', 'and', 'for', 'CS']

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!