Python | Finding strings with given substring in list
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 comprehension
List 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 substring subs = '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() + lambda
This 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 substring subs = '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 substring subs = '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']
Recommended Posts:
- Python | Filter list of strings based on the substring list
- Python | Replace substring in list of strings
- Python | Check if substring is part of List of Strings
- Python | Finding frequency in list of tuples
- Python | Finding relative order of elements in list
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python | Duplicate substring removal from list
- Python | Get the substring from given string using list slicing
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list of tuples to list of strings
- Python | Convert List of lists to list of Strings
- Python | Convert list of strings to list of tuples
- Python | Filter a list based on the given list of strings
- Python | Scramble strings in list
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.


