In this Python tutorial, we will discuss several methods to find a string from a list in Python. Moreover, we’ll look at a variety of examples related to it.
As a Developer, while making the Python Project I got the requirement to find a string in a Python List.
Here we will see:
- Find a String from a List in Python using for loop
- How to find a string from a list in Python using any()
- Find string from a list using count() in Python
- How to find a string from a list in Python using list comprehension
- Find a string from a Python list using in operator
- How to find a string from a list in Python using filter and lambda
- Find a string from a Python list using a regular expression
Find a String from a List in Python
There are different ways, we can find a string in a list in Python like for loop, in operator, using count, any() function.
Find a String from a List in Python using for loop
- In this example, the items of a string will be combined into a list. The user will next provide us with an input string, which we will utilize to see if it contains any items in a list or not. We will locate the string in a list using a for loop.
- Python’s for loop is used to continuously loop through a list, tuple, string, or another iterable object.
Example:
Let’s take an example and check how to find the string in a Python list.
Source Code:
cities_of_USA = ['New York', 'Los Angeles', 'California']
new_val = input("Enter the given str : ")
m = 0
#Using for loop
for n in cities_of_USA:
if n == new_val:
m = 1
break
if m== 1:
print("String is available")
else:
print("String is not available")
- First, we’ll create a list with an element in the form of a string in it. The user’s input will then be taken in the form of a string.
- We’ll use the variable m, which is initialized to 0, and then apply for loop after that.
- The if condition, which determines whether an item of the list is equal to the string, will be used inside the for loop. If they are equal, the loop is terminated, and the value of m changes to 1.
- Finally, we shall determine whether m equals 1 or 0. We will output string is present if m is equal to 1 and the string is not present if m is equal to 0.
Here is the implementation of the following given code.
This is how to find the string from a Python list by using the for-loop method.
Read: Python list comprehension lambda
How to find a string from a list in Python using any() method
- In this section, we will discuss how to find the string from a list in Python by using the any() method.
- To perform this particular task we are going to use the any() method and the Python function any() takes an iterable as an argument and returns true if any of the iterable’s elements are available, else it returns false. Any() method returns false if the iterable is empty.
- In this example, we will check the existence of the given string in any element of the list.
Syntax:
Let’s have a look at the syntax and understand the working of any() method in Python
any(iterable)
Note: This function takes iterable items in an argument.
Country_name = ["U.S.A", "Australia", "Belgium"]
if any("Australia" in str for str in Country_name):
print("Input string contain in list")
else:
print("Input string does not contain in list")
In the following given code first, we created a list and assigned the string elements to it. Next, we used the any() function with the if condition and it will check the condition whether the string is present or not in the list.
Here is the Screenshot of the following given code.
As you can see in the Screenshot we have discussed how to find the string from a list in Python by using any() method.
Read: How to trim a string in Python
Find string from a list in Python using count()
- In this example, we will discuss how to find the string from a list in Python by using the count() method.
- The count() function is used to determine how many times a string appears in a list. The string does not contain the list, according to the output of 0, but still, the string does contain the list, according to the output of 1.
- In this example, we will create a list with items of a string and take an input user which will help the user to find the string in a list or not.
- Next, we used the count() function and within this function, we will pass the input string and it will return the count value through the if condition.
Example:
Let’s take an example and check how to find the string from a list in Python by using the count() method.
Source Code:
#input list
Bikes_in_USA = ['Aprilia', 'Kawasaki', 'Ducati']
#input string from user
input_val = input("Enter the string val : ")
#By using the count function
new_result = Bikes_in_USA.count(input_val)
if new_result > 0:
print("String is available")
else:
print("String is not available")
In the above code first, we created the list ‘Bikes_in_USA’ and assigned the items to the list, and then used the input() function from the input user. Next, we used the count() function, and within this function, we passed the list to it.
Here is the implementation of the following given code.
In this example, we have understood how to find the string from a list by using the count method in Python.
Read: Python split string by space
How to find a string from a list in Python using list comprehension
- Here we will discuss how to find the string from a list in Python by using the list comprehension method.
- By using the list comprehension and join method we can easily involve splitting each string element into its associated list element, then joining each of them to declare a new string.
- Let’s consider a different example where you simply want to determine whether a string is a substring of another word in the list and then return all such words.
Example:
Let’s take an example and check how to find the string from a list in Python by using the list comprehension method.
Source Code:
Country_name = ["United States of America","Germany"]
new_output = [i for i in Country_name if "Germany" in i]
print("String is available :",new_output)
In the above code first, we created the list ‘Country_name’ and then used the list comprehension method.
Here is the Screenshot of the following given code
This is how to find the string from a list in Python by using the list comprehension method.
Also, check: Python list comprehension using if-else
Find a string from a list in Python using IN operator
- In this section, we will discuss how to find the string from a list in Python.
- To find a string in a list in Python, use the in operator. This accepts the operands a and b and has the following form.
- In this example, the items of a string will be converted into a list. If we provide the string to be searched and determine whether it appears in the list or not for this, we will use the in operator.
Syntax:
result= a in b
Example:
Let’s take an example and check how to find the string from a list in Python.
Source Code:
cities_of_USA = ['New York', 'Los Angeles', 'California']
search_val = 'Los Angeles'
if search_val in cities_of_USA:
print('Los Angeles is available!')
else:
print('Los Angeles is not available')
In the following given code first, we created the list ‘cities_of_USA’ and then declare the variable ‘search_val’, and then check the condition if the string value is available or not.
Here is the implementation of the following given code.
In this example, we have understood how to search the string from a list in Python by using the in operator.
Read: Python string to list
How to find string from a list in Python by using filter and lambda
- In this section, we will discuss How to find the string from a list using the filter and lambda in Python.
- To perform this particular task we are going to use the filter() method and a lambda function, which is a basic function that is only declared on that specific line, using the filter() method.
Example:
Let’s take an example and check how to find the string from a list by using the filter and lambda in Python.
Source Code:
# Input list
Country_name = ['U.S.A', 'China', 'Germany']
search_val = filter(lambda m: 'U.S.A' in m, Country_name)
# Convert the object into list
result=list(search_val)
# Display the Content
print("Searched string value from list :", result)
In the above code first, we created the input list and then used the filter and lambda function and iterate the values from the list.
Here is the Screenshot of the following given code
As you can see in the Screenshot we have discussed How to find the string from a list in Python by using the filter and lambda.
Read: Python loop through a list
Find a string from a list in Python using a regular expression
- Here we will discuss how to find the string from a list in Python by using the regular expression method in Python.
- The first thing to understand when using a Python regular expression is that everything is essentially a character and that we are creating patterns to match a certain string of characters.
- The re.search() method is used When the pattern is found, the Python re.search() function provides a match object; otherwise, it returns “null.”
Example:
Let’s take an example and check how to find the string from a list in Python by using the regular expression method in Python.
Source Code:
Bikes_in_USA = ['Aprilia', 'BMW', 'Ducati']
import re
result=re.compile(r'il')
new_output = [m for m in Bikes_in_USA if re.search('il', m)]
print(new_output)
In the following given code first, we created the list named ‘Bikes_in_USA’ then we used the list comprehension method and assigned the re.search(). It will check the condition if the given pattern is available or not.
You can refer to the below Screenshot
Also, take a look at some more Python tutorials.
- Python program for a diamond pattern
- How to find smallest number in a Python list
- Remove a specific character from a string in Python
- How to Python Append List to Another List
- How to Sum Elements in List in Python using For Loop
This article was based on how to find a string from a list in Python. Seven different approaches have been presented and all the methods are very simple and easy to understand.
- Find a String in a List in Python using for loop
- How to find a string from a list in Python using any() method
- Find string from a list using count() in Python
- How to find a string from a list in Python using list comprehension()
- Find a string from a Python list using in operator
- How to find a string from a list in Python using filter and lambda
- Find a string from a Python list using a regular expression
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.