Python | Finding strings with given substring in list

24 Dec, 2018
The classical problem that can be handled quite easily by Python and has been also dealt with many times is finding if a string is substring of other. But sometimes, one wishes to extend this on list of strings, and hence then requires to traverse the entire container and perform the generic algorithm.
Let’s discuss certain ways to find strings with given substring in list.
Method #1 : Using list comprehensionList comprehension is an elegant way to perform any particular task as it increases readability in a long run. This task can be performed using naive method and hence can be reduced to list comprehension as well.
# Python code to demonstrate # to find strings with substrings # using list comprehension   # initializing list test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']  # printing original list print ("The original list is : " + str(test_list))  # initializing substringsubs = 'Geek'  # using list comprehension # to get string with substring res = [i for i in test_list if subs in i]  # printing result print ("All strings with given substring are : " + str(res))
The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
All strings with given substring are : ['GeeksforGeeks', 'Geeky']

 Method #2 : Using filter() + lambdaThis function can also perform this task of finding the strings with the help of lambda. It just filters out all the strings matching the particular substring and then adds it in a new list.
# Python code to demonstrate # to find strings with substrings # using filter() + lambda  # initializing list test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']  # printing original list print ("The original list is : " + str(test_list))  # initializing substringsubs = 'Geek'  # using filter() + lambda # to get string with substring res = list(filter(lambda x: subs in x, test_list))  # printing result print ("All strings with given substring are : " + str(res))
The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
All strings with given substring are : ['GeeksforGeeks', 'Geeky']

 Method #3 : Using re + search()Regular expressions can be used to perform many task in python. To perform this particular task also, regular expressions can come handy. It finds all the matching substring using search() and returns result.
# Python code to demonstrate # to find strings with substrings # using re + search()import re  # initializing list test_list = ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']  # printing original list print ("The original list is : " + str(test_list))  # initializing substringsubs = 'Geek'  # using re + search()# to get string with substring res = [x for x in test_list if re.search(subs, x)]  # printing result print ("All strings with given substring are : " + str(res))
The original list is : ['GeeksforGeeks', 'Geeky', 'Computers', 'Algorithms']
All strings with given substring are : ['GeeksforGeeks', 'Geeky']

Python list-programs
substring
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Convert string dictionary to dictionary