How to use operator.contains() method In Python

Approach

  1. Initiate a for loop to traverse list of strings and set res to False
  2. Check whether each string of list is present in given string
  3. If yes set res to True and break out of for loop
  4. Display res

Python3




# Python3 code to demonstrate
# checking if string contains list element
# initializing string
import operator as op
test_string = "There are 2 apples for 4 persons"
 
# initializing test list
test_list = ['apples', 'oranges']
 
# printing original string
print("The original string : " + test_string)
 
# printing original list
print("The original list : " + str(test_list))
 
# checking if string contains list element
res = False
c = 0
for i in test_list:
    if(op.contains(test_string, i)):
        res=op.contains(test_string, i)
        break
 
# print result
print("Does string contain any list element : " + str(res))


Output

The original string : There are 2 apples for 4 persons
The original list : ['apples', 'oranges']
Does string contain any list element : True

Time Complexity : O(M*N) M-length of test_list N-length of test_string

Auxiliary Space : O(1)

Approach#9:using lambda

Algorithm

1. Take the input string and a list of words from the user.
2. Define a lambda function contains_word() using lambda, map() and any() functions to check if any element from the list is present in the string.
3. Call the contains_word() function with the input string and the list of words as arguments.
4. Print the result.

Python3




string ="There are 2 apples for 4 persons"
lst =  ['apples', 'oranges']
 
# using lambda functions to check if string contains any element from list
contains_word = lambda s, l: any(map(lambda x: x in s, l))
 
# printing the result
if contains_word(string, lst):
    print("String contains at least one word from list.")
else:
    print("String does not contain any word from list.")


Output

String contains at least one word from list.

Time complexity: O(n*m) where n is the length of the input string and m is the length of the list of words. This is because we are using the in operator to check if each element of the list is present in the string, which takes linear time.

Space complexity: O(n) where n is the length of the input string. This is because we are storing the input string in memory. The space used by the list of words and the lambda functions is negligible compared to the input string.



Python | Test if string contains element from list

We are given a String and our task is to test if the string contains elements from the list.

Example:

Input:    String: Geeks for Geeks is one of the best company.
        List: ['Geeks', 'for']

Output:    Does string contain any list element : True

Similar Reads

Naive Approach checking each word in the string

Here we are splitting the string into list of words and then matching each word of this list with the already present list of words we want to check....

Using list comprehension to check if string contains element from list

...

Using any() to check if string contains element from list

This problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements....

Using find() method to check if string contains element from list

...

Using Counter() function

Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list....

Using operator.contains() method

...

Contact Us