How to find a string from a list in Python [7 Methods]

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 Python Developer, while making the Python Project I got the requirement to find a string in a Python List. So, I came across some methods present in Python that can be used.

Methods to Find a String from a List in Python

There are different ways present in Python, we can find a string in a list in Python:

  • for loop
  • any() function
  • count() function
  • list comprehension
  • in operator
  • filter() with lambda function
  • regular expression

Let’s see them one by one with some demonstrative examples:

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 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 Python 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 Python 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 in Python.
  • 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 in Python.

Here is the implementation of the following given Python code.

#Case 1
Enter the given str : New York
String is available

#Case 2
Enter the given str : Paris
String is not available
How to find a string from a list in Python
Python list contains a string

This is how to find the string from a Python list by using the for-loop method.

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 elements are available, it returns True. The any() method returns False if the iterable is empty. That means the any() function returns boolean values in Python.
  • In this example, we will check the existence of the given string in any element of the Python list.

Syntax:

Let’s have a look at the syntax and understand the working of any() method in Python

any(iterable)

Note: This Python 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")

Output: In the following given code of Python first, we created a Python 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 Python list.

Input string contain in list

Here is the Screenshot of the following given Python code:

python find string in list

As you can see in the Screenshot we have discussed how to find the string from a list in Python by using any() method.

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 list does not contain the string, according to the output of 0, but still, the Python list does contain the string, 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 Python 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")

Output: In the above Python code first, we created the list ‘Bikes_in_USA’ in Python and assigned the items to the list, and then used the input() function to take the input from the user. Next, we used the count() function, and within this function, we passed the Python list to it.

Here is the implementation of the following given Python code:

#Case 1
Enter the string val : Ducati
String is available

#Case 2
Enter the string val : BMW
String is not available
count function in python
count function python

In this example, we have understood how to find the string from a list by using the count() method in Python.

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 string in the Python list and then return all such string.

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]
if len(new_output) > 0:
    print("String is available :", new_output)
else:
    print("String is not available")

Output: In the above code first, we created the Python list ‘Country_name’ and then used the list comprehension method.

String is available : ['Germany']

Here is the Screenshot of the following given code of Python:

how to find a string in a list python

Case 2:

Country_name = ["United States of America", "Germany"]

new_output = [i for i in Country_name if "New York" in i]
if len(new_output) > 0:
    print("String is available :", new_output)
else:
    print("String is not available")

Output:

String is not available
find a string in Python list

This is how to find the string from a list in Python by using the list comprehension method.

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 in Python. If we provide the string to be searched and determine whether it appears in the Python 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(f'{search_val} is available!')
else:
    print(f'{search_val} is not available')

Output: In the following given code first, we created the Python list ‘cities_of_USA’ and then declared the variable ‘search_val’, and then checked the condition if the string value is available or not.

Here is the implementation of the following given Python code:

Los Angeles is available!
find function python

In this example, we have understood how to search the string from a list in Python by using the in operator.

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 functions 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 functions 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)
print("Searched string value from list :", result)

Output: In the above code first, we created the input list in Python and then used the filter and lambda function and iterate the values from the Python list.

Searched string value from list : ['U.S.A']

Here is the Screenshot of the following given Python code:

python codes list

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 functions.

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 [].

Example:

Let’s take an example and check how to find the string from a list in Python by using the regular expression module functions like compile() and search() 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)

Output: In the following given code first, we created the list in Python 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.

#Case 1
['Aprilia']

#Case 2
[]

You can refer to the below Screenshot:

find a string in a list python
python input string

Also, take a look at some more Python tutorials.

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.