Given a list, the task is to write a program that checks is their any element in the list with string data type is present in the list or not, by using the methods or ways provided in Python.
Examples:
Input: l=[1, 1.0, 'have', 'a', 'geeky', 'day']; s='geeky'
Output: True
Explanation: geeky is present in the input list
Find the String in a List in Python
Below are the methods that we will cover in this article:
- Using in operator
- Using count() function
- Using List Comprehension
- Using any() function
- Using map() with find() methods
- Using operator methods
- Using try/except and index()
- Using re in Python
- Using numpy library
Find a string in a list using the ‘in’ operator
The in operator comes 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.
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)
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, 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.
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)
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.
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
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.
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)
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.
Python3
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' ) |
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
Python3
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: 2
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
Python3
# 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' ) |
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 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' ) |
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.
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.
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. |
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.
Find the string in the list using numpy library
Import the numpy module then initialize a list variable l with some string values. Initialize a string variable s with the value “prime”.Use a list comprehension to create a list of boolean values, where each value corresponds to whether the string s is present in the corresponding element of the list l.Use the np.any() method to check if any of the values in the resulting list is True. Print a message indicating whether the string s is present in the list or not, based on the value of res.
Python3
import numpy as np # assign list l = [ 'hello' , 'geek' , 'have' , 'a' , 'geeky' , 'day' ] # assign string s = 'prime' # check if string is present in list res = np. any ([s in i for i in l]) if res: print (f '{s} is present in the list' ) else : print (f '{s} is not present in the list' ) #This code is contributed by Jyothi pinjala. |
Output:
prime is not present in the list
Time complexity: O(nm), where n is the length of the input list and m is the length of the input string. The list comprehension has a time complexity of O(nm), as it iterates over each element of the list and checks if the string s is present in it.
Auxiliary Space: O(nm), where n is the length of the input list and m is the length of the input string. The list comprehension creates a new list of boolean values with a length of n, and each value in the list is a string of length m. Hence, the space complexity is O(nm).