How to check if Python list contains string
Introduction
Given an input list of strings and a search keyword. We want to check if the search keyword is found in the input list (not necessarily an exact match). In other words, if the search keyword is a substring of at least one string in the input list then it is considered a match.
Check substring
In Python, to check if a string is a substring of another, we can do that in multiple ways. For example using the (in) operator…
1 2 |
# This is going to print: True print("test" in "this-is-a-test") |
We can also use the find function against a string as follows…
1 2 3 4 |
# This is going to print 10 which is the # position of test otherwise if not found # it prints -1 print('this-is-a-test'.find('test')) |
In this post, we are going to use the first method. Back to our main question…
Check list contains substring
Consider the following input list, we want to search for the keyword ‘cisco’ using different methods…
1 2 |
input_list = ['1-cisco-san-jose', '2-microsoft-seatle', '3-cisco-sf', '4-google-mountain-view'] |
- Iterate through the list in a classical way…
1 2 3 4 5 6 7 8 9 10 11 |
# Returns True if string is found in lst def list_contains_string(lst, string): for v in lst: if string in v: return True return False # This is going to print: True print(list_contains_string(input_list, 'cisco')) # This is going to print: False print(list_contains_string(input_list, 'xyz')) |
- A more pythonic way is to use a generator expression with the (any) operator. Here is how to do that…
1 2 3 4 5 6 7 8 |
# Returns True if string is found in lst def list_contains_string(lst, string): return any(string in v for v in lst) # This is going to print: True print(list_contains_string(input_list, 'cisco')) # This is going to print: False print(list_contains_string(input_list, 'xyz')) |
To understand what that means, the expression
1 |
string in v for v in lst |
is called a generator expression. It is an object, it must be invoked to iterate through all of its items on demand. For example…
1 2 3 4 5 6 7 |
# This is going to print: # True # False # True # False for x in ('cisco' in v for v in input_list): print(x) |
When using (any) operator with this generator expression, if any value is True, the final result is True as well like a logical OR.
- Instead of using a generator expression, we can also use a list comprehension. Take a look…
1 2 3 4 5 6 7 8 |
# Returns True if string is found in lst def list_contains_string(lst, string): return len([x for x in lst if string in x]) > 0 # This is going to print: True print(list_contains_string(input_list, 'cisco')) # This is going to print: False print(list_contains_string(input_list, 'xyz')) |
What does that mean ? Well, the expression
1 |
[x for x in lst if string in x] |
is called a list comprehension, it produces a new list where all items contain the search keyword. If the length is greater than zero, that means at least one string in the input list contains the search keyword so it returns True.
- Another way to to check if the search keyword exists in the input list is to convert the input list into a long string then use the (in) operator. Take a look…
1 2 |
# This should print: True print('cisco' in str(input_list)) |
Finding all matches
So far we were trying to find if there is a match. To print the actual matches, we can do that in a one liner as follows…
1 2 3 4 |
# This should print: # 1-cisco-san-jose # 3-cisco-sf print([x for x in input_list if 'cisco' in x]) |
References
For more information about generators in Python you may check the following article
Thanks for visiting. Please use the comments section below for questions.