In this article, we’ll look at how to find a string in a list in Python with various code examples.
To determine whether a string exists in a list, simply use the “in” operator. However, I will also discuss other approaches to this problem statement that you can use.
Find A String In A List In Python
There are several approaches to this problem, ranging from ease of use to the efficiency of the solution.
1. Using in Operator
In Python, we can use the in operator to find a string within a list of strings. This takes in two operands, stringVariable and listVariable, and has the following syntax:
Syntax:
isStringFound = stringVariable in listVariable
Where,
stringVariable = is the variable that stores the string.
listVariable = is the variable that stores the list.
Here StringFound is a boolean variable that stores either true or false depending on whether or not the string is present in the list.
Let us take the usage of the “in” operator in the below code example to search a string in a list in Python.
#Initializing List With String inputList = ["A", "B", "C"] #Checking if "A" is present in List or Not if "A" in inputList: print("A is found in the List.") else: print("A is not found.")
Output:
A is found in the List.
To simplify and check various other inputs, we can write a function that checks whether or not the input string is present in the list.
#Creating a Function def checkIfStringPresent(searchString, inputList): if searchString in inputList: return "{} is Present in the List.".format(searchString) else: return "{} is not present in the List.".format(searchString) #Initializing List With String List1 = ["A", "B", "C"] #Checking if "B" is present in List or Not print(checkIfStringPresent("B", List1)) #Checking if "D" is present in the list or not print(checkIfStringPresent("D", List1))
Output:
B is Present in the List.
D is not present in the List.
This is the most common and recommended method for searching for a string in a list. However, for illustration purposes, we will also show you other methods.
2. Using List Comprehension Method
As an example, consider the situation in which you only want to check if the string is a substring of another word on the list and then return all of the words in which your string is a substring of the other word on the list.
Let us see in the below code example how you can search a string in a list using the List comprehension method.
#Creating a Function def checkIfStringPresent(searchString, inputList): #Using List Comprehension method foundStringList = [item for item in inputList if searchString in item] return foundStringList #Initializing List With String List1 = ["You Are On Coduber", "Coduber Is Coding", "Code"] #Checking if "Coduber" is present in List or Not print(checkIfStringPresent("Coduber", List1)) #Checking if "D" is present in the list or not print(checkIfStringPresent("D", List1))
Output:
['You Are On Coduber', 'Coduber Is Coding']
[]
As you can see in the above code, the coduber string appeared twice in the list, and our return list contains both strings that contain our search string.
3. Using any() Method
The any() can be used to determine whether or not the input string exists in any item of the list, which is useful if you want to check for the existence of the input string in any item of the list.
#Initializing List With String List1 = ["You Are On Coduber", "Coduber Is Coding", "Code"] if any("Coduber" in word for word in List1): print("Coduber is present in the List.") else: print("Coduber is not Found in the List.")
Output:
Coduber is present in the List.
4. Using filter and Lamdas Method
Additionally, the filter() method can be used to filter out functions that are only defined on one particular line, such as lambda functions.
Consider lambda to be a mini function that cannot be called again after it has been called.
Let us see in the below code example usage of the filter method along with lambda in Python.
#Initializing List With String List1 = ["You Are On Coduber", "Coduber Is Coding", "Code"] #Using the Filter Object filterObj = filter(lambda a:"Coduber" in a, List1) #Converting the Filter Object in List inList = list(filterObj) print(inList)
Output:
['You Are On Coduber', 'Coduber Is Coding']
We have exactly what we were hoping for. When we ran our filter function, we only got one string back, and that is exactly what we wanted.
Wrap Up
I hope you learned how to find a string in a list of strings in Python. I’ve listed around four methods for you to use to find the string.
You can use any of them depending on your needs, but the first method is the most recommended.
Let me know in the comment section if you know any better method than the one discussed above, I will be happy to add it here.
Further Read: