Change language

Python | Check if a string contains an element from a list

|

Method # 1: Using List Comprehension
This problem can be solved by using list comprehension, in this we check the list, and also by using string elements if we can find a match. and return true if we find one and false does not use conditional statements.

# Python3 demo code
# check if the string contains a list item
# use comprehension list

 
# initialization string

test_string = " There are 2 apples for 4 persons "

  
# initializing the test list

test_list = [ ’apples’ , ’oranges’ ]

 
# print original line

print ( "The original string:" + test_string)

 
# print the original list

print ( "The original list:" + str (test_list))

 
# using comprehension list
# check if the string contains a list item

res = [ele for ele in test_list if (ele in test_string)]

  
# print result

print ( " Does string contain any list element: " + str ( bool (res)))

Output:

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

Method # 2: using any()
Using any function is the most classic way in which you can accomplish this task, as well as efficiently. This function checks for a match in line with the match of each list item.

# Python3 demo code
# check if string contains a list item
# use comprehension list

 
# initializing string

test_string = "There are 2 apples for 4 persons"

 
# initialize the test list

test_list = [ ’apples’ , ’ oranges ’ ]

 
# print original line

print ( " The original string: " + test_string)

 
# print the original list

print ( "The original list : " + str (test_list))

 
# using comprehension list
# check if the string contains a list item

res = any (ele in < / code> test_string for ele in test_list)

 
# 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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically