Skip to content
Related Articles

Related Articles

Python | Check if substring is part of List of Strings

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Aug, 2022
View Discussion
Improve Article
Save Article

Many problems of substrings have been dealt with many times. There can also be such problem in which we require to check if argument string is a part of any of the strings coming in the input list of strings. Let’s discuss various ways in which this can be performed. 

Method #1 : Using join() The basic approach that can be employed to perform this particular task is computing the join of all the list strings and then searching the string in the joined string. 

Python3




# Python3 code to demonstrate working of
# Check if substring is part of List of Strings
# Using join()
 
# initializing list 
test_list = ['GeeksforGeeks', 'is', 'Best']
 
# test string
check_str = "for"
 
# printing original string
print("The original string is : " + str(test_list))
 
# Using join()
# Check if substring is part of List of Strings
temp = '\t'.join(test_list)
res = check_str in temp
 
# printing result
print("Is check string part of any input list string : " + str(res))

Output : 

The original string is : ['GeeksforGeeks', 'is', 'Best']
Is check string part of any input list string : True

Method #2 : Using any() The any function can be used to compute the presence of the test substring in all the strings of the list and return True if it’s found in any. This is better than the above function as it doesn’t explicitly take space to create new concatenated string. 

Python3




# Python3 code to demonstrate working of
# Check if substring is part of List of Strings
# Using any()
 
# initializing list 
test_list = ['GeeksforGeeks', 'is', 'Best']
 
# test string
check_str = "for"
 
# printing original string
print("The original string is : " + str(test_list))
 
# Using any()
# Check if substring is part of List of Strings
res = any(check_str in sub for sub in test_list)
 
# printing result
print("Is check string part of any input list string : " + str(res))

Output : 

The original string is : ['GeeksforGeeks', 'is', 'Best']
Is check string part of any input list string : True

Method #3: Using find() method

Python3




# Python3 code to demonstrate working of
# Check if substring is part of List of Strings
 
# initializing list
test_list = ['GeeksforGeeks', 'is', 'Best']
 
# test string
check_str = "for"
 
# printing original string
print("The original string is : " + str(test_list))
 
res=False
# Check if substring is part of List of Strings
for i in test_list:
    if(i.find(check_str)!=-1):
        res=True
# printing result
print("Is check string part of any input list string : " + str(res))

Output

The original string is : ['GeeksforGeeks', 'is', 'Best']
Is check string part of any input list string : True

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!