Skip to content
Related Articles
Open in App
Not now

Related Articles

Python program to find the String in a List

Improve Article
Save Article
  • Last Updated : 21 Mar, 2023
Improve Article
Save Article

Given a list, the task is to write a Python program to check whether a list contains a particular string or not.

Examples:

Input: l=[1, 1.0, 'have', 'a', 'geeky', 'day']; s='geeky'
Output: geeky is present in the list
Input: l=['hello',' geek', 'have', 'a', 'geeky', 'day']; s='nice'
Output: nice is not present in the list

Method #1: Using in operator

The in operator comes handy for checking if a particular string/element exists in the list or not.

Example:

Python3




# 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

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

Method #2: Using count() function

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, it means that a particular string exists in the list, else that string doesn’t exist in the list.

Example:

Python3




# 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), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), no extra space is required

Method #3: 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.

Example:

Python3




# 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:

geeky is present in the list

Method #4: 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.

Example:

Python3




# 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

The time and space complexity for all the methods are the same:

Time Complexity: O(n) -> as the built-in operators and functions like ‘in’, ‘count’ take O(n)

Space Complexity: O(n)

Method #5 : Using  list(),map(),join(),find() methods

Python3




# assign list
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) -> built-in functions like join takes O(n)

Space Complexity: O(n)

Method #6 : Using Counter() function

Python3




from collections import Counter
# assign list
l = [1, 2.0, 'have', 'a', 'geeky', 'day']
 
# assign string
s = 'geeky'
 
freq = Counter(l)
# check if string is present in the list
if s in freq.keys():
    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) 

Auxiliary Space: O(n)

Method 7:  using operator.countOf() method

Python3




import operator as op
# assign list
l = ['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')

Output

prime is not present in the list

Time Complexity: O(N)

Auxiliary Space : O(1)

Method :  using try/except and 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.

Python3




# 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) where n is the number of elements in the list, as the index() method iterates over the elements of the list until it finds the desired string or it exhausts the list.

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

Method : Using re

Algorithm

  1. Initialize a list l and a string s.
  2. Import the re module.
  3. Use the re.search() function to search for the string s in the list l.
  4. If a match is found, print that the string s is present in the list.
  5. If no match is found, print that the string s is not present in the list.

Python3




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), where n is the number of items in the list and m is the length of the longest string in the list.
Auxiliary Space: O(1), as we are not using any additional data structures in the program.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!