Open In App

Python program to find String in a List

Last Updated : 11 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a list, the task is to find String in a List, by using the methods or ways provided in Python.

Example: Find String in a List Using the ‘in’ Operator

Python
# assign list
l = [1, 2.0, 'have', 'a', 'geeky', 'day']

# assign string
s = 'geeky'  

# check if string is present in the list
if s in l:
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
geeky is present in the list

Explanation: This comes in handy for checking whether a particular string/element exists in the list. So here is the code where we use a conditional statement with ‘in’ operator to check if we have the particular string in the list and the conditional operator returns boolean values according to it.

Find String in List using count() method

The count() function is used to count the occurrence of a particular string in the list. If the count of a string is more than 0 in Python list of strings, it means that a particular string exists in the list, else that string doesn’t exist in the list. In the given code we use the we use the conditional statement to check whether the particular string is present or not if it is not present the count function returns 0.

Python
# assign list
l = ['1', 1.0, 32, 'a', 'geeky', 'day']

# assign string
s = 'prime'

# check if string is present in list
if l.count(s) > 0:
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
prime is not present in the list

Time Complexity: O(n)
Auxiliary Space: O(1)

Find a String in a List Using List Comprehension

List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. It is used to transform iterative statements into formulas. In this, we create a list to store the string which we have to find in the list and then store it in the new list, and then we can check whether the new list is empty.

Python
# assign list
l = ['hello', 'geek', 'have', 'a', 'geeky', 'day']

# assign string
s = 'geek'

# list comprehension
compare = [i for i in l if s in l]

# check if string is present in list
if len(compare) > 0:
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
geek is present in the list

Time Complexity: O(n) 
Space Complexity: O(n)

Find the String in the List Using any() Function

The any() function is used to check the existence of an element in the list. it’s like- if any element in the string matches the input element, print that the element is present in the list, else, print that the element is not present in the list.

Python
# assign list
l = ['hello', 'geek', 'have', 'a', 'geeky', 'day']

# assign string
s = 'prime'

# check if string is present in list
if any(s in i for i in l):
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
prime is not present in the list

Time Complexity: O(n)
Space Complexity: O(n)

Find String in a List Using map() and find() Methods

Here with map() we convert every datatype of the list into a string and then join them and make a single string then with the help of find() function we search the string in it.

Python
l = [1, 2.0, 'have', 'a', 'geeky', 'day']
# assign string
s = 'geeky'
nl=list(map(str,l))
x=" ".join(nl)
# check if string is present in the list
if x.find(s)!=-1:
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
geeky is present in the list

Time Complexity: O(n) 
Space Complexity: O(n)

Find the String in a List Using operator methods

Example 1: Using operator.countOf() method

In Python the operator module we have cout.Of() method which returns the number of occurrences of the particular element which presents in the list and if not present then returns 0 so through this we can also find the string in a list

Python
import operator as op
# assign list
l = ['1','1', 1.0, 32, 'a', 'geeky', 'day']

# assign string
s = 'prime'

# check if string is present in list      
if op.countOf(l, s):
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

#it return the number of occurence of element in the list
print('The no. of occurrence of "1" in the list is:',op.countOf(l,'8'))

Output
prime is not present in the list
The no. of occurrence of "1" in the list is: 0

Time Complexity: O(N)
Auxiliary Space : O(1)

Example 2: Using operator.contains()

Check whether the given string is present in the list using operator.contains(). If the yes display “string” is present in the  list else display “string” is not present in the list

Python
# assign list
import operator
l = [1, 2.0, 'have', 'a', 'geeky', 'day']
# assign string
s = 'geeky'
# check if string is present in the list
if operator.contains(l, s):
    print(f'{s} is present in the list')
else:
    print(f'{s} is not present in the list')

Output
geeky is present in the list

Time Complexity : O(n) n – length of list
Auxiliary Space : O(1)

Find a String in a List Using index()

You can use the index() method to find the first index of a string in a list. If the string is present in the list, the index() method returns the first index of the string otherwise, it raises a ValueError. To check if a string is present in a list, you can wrap the index() method in a try-except block, and print a message indicating whether the string is present in the list or not.

Python
# assign list
l = [1, 2.0, 'have', 'a', 'geeky', 'day']
 
# assign string
s = 'geeky'
 
try:
    # check if string is present in list
    index = l.index(s)
    print(f'{s} is present in the list at index {index}')
except ValueError:
    print(f'{s} is not present in the list')

Output
geeky is present in the list at index 4

Time Complexity: O(n)
Auxiliary Space: O(1), as the method uses only a few constant-sized variables, regardless of the size of the list.

Find the String in the List Using re module

Initialize a list l and a string s then import the re module. Use the re.search() function to search for the string s in the list l If a match is found, print that the string s is present in the list If no match is found, print that the string s is not present in the list.

Python
import re

# assign list
l = [1, 2.0, 'have', 'a', 'geeky', 'day']

# assign string
s = 'geeky'

# check if string is present in each string in the list
for item in l:
    if isinstance(item, str) and re.search(s, item):
        print(f'{s} is present in the list')
        break
else:
    print(f'{s} is not present in the list')
# This code is contributed by Vinay Pinjala.

Output
geeky is present in the list

Time complexity: O(n*m),
Auxiliary Space: O(1)

Python program to find the String in a List – FAQs

How to Find a String in a List in Python

To find a string in a list in Python, you can use the in keyword which checks if the string exists in the list and returns True or False based on its presence.

Example:

my_list = [‘apple’, ‘banana’, ‘cherry’] search_string = ‘banana’ found = search_string in my_list print(found) # Output: True

How to Check if a String Exists in a List in Python

Checking if a string exists in a list can be efficiently done using the in keyword as demonstrated above. This is the most straightforward method for existence checking.

How Does find() Work in Python?

The find() method is used with strings to locate the position of a substring within a string. It returns the lowest index of the substring if it is found in the string. If it is not found, it returns -1.

Example:

text = “Hello world” index = text.find(“world”) print(index) # Output: 6

How Do You Check if Any String in List Starts With in Python?

To check if any string in a list starts with a certain substring, you can use a loop along with the startswith() method of the string class.

Example:

my_list = [‘apple’, ‘banana’, ‘cherry’] prefix = ‘ba’ found = any(s.startswith(prefix) for s in my_list) print(found) # Output: True

How Do I Find a String Containing a Substring in a List in Python?

To find a string containing a specific substring within a list, you can use list comprehension combined with the in keyword or the find() method for more complex conditions.

Example using list comprehension with in:

my_list = [‘apple pie’, ‘banana bread’, ‘cherry tart’] substring = ‘pie’ containing_strings = [s for s in my_list if substring in s] print(containing_strings) # Output: [‘apple pie’]

These methods provide versatile ways to search for strings and substrings within lists, accommodating a wide range of search conditions in Python



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: