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']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Filter list of strings based on the substring list
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Python - Filter the List of String whose index in second List contaons the given Substring
- Python | Check if substring is part of List of Strings
- Python | Replace substring in list of strings
- Python - Count Strings with substring String List
- Python - Substring presence in Strings List
- Python - All occurrences of Substring from the list of strings
- Minimum length of substring whose rotation generates a palindromic substring
- Python | Filter a list based on the given list of strings
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Sort the Array of Strings on the basis of given substring range
- Python | Finding frequency in list of tuples
- Python | Finding relative order of elements in list
- Python | Get the substring from given string using list slicing
- Python program to sort strings by substring range
- Python | Convert list of strings and characters to list of characters
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
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.

