# Python | Check if substring is part of List of Strings

Last Updated : 03 May, 2023

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```

Time Complexity: O(n), where n is the total length of all the strings in the input list.

Auxiliary Space: O(n)

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```

Method #4: Using filter() and lambda function

## Python3

 `# Python3 code to demonstrate working of` `# Check if substring is part of List of Strings` `# Using filter() and lambda function`   `# initializing list` `test_list ``=` `[``'GeeksforGeeks'``, ``'is'``, ``'Best'``]`   `# test string` `check_str ``=` `"for"`   `# printing original string` `print``(``"The original string is : "` `+` `str``(test_list))`   `# Using filter() and lambda function` `# Check if substring is part of List of Strings` `res ``=` `any``(``filter``(``lambda` `x: check_str ``in` `x, test_list))`   `# printing result` `print``(``"Is check string part of any input list string : "` `+` `str``(res))` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

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

Time complexity: O(n) where n is the length of the input list of strings. This is because we are iterating through the list once to check if the substring is present in each element of the list.
Auxiliary space:  O(1) as we are only using a few variables to store the substring, the input list, and the result.

Method 5: Using a simple loop to iterate over the list and check if the substring is present in each string.

• Define a list test_list with three string elements – ‘GeeksforGeeks’, ‘is’, and ‘Best’.
• Define a string check_str as ‘for’.
• Print the original string by concatenating the string ‘The original string is : ‘ with the string representation of test_list.
• Initialize a boolean variable res to False.
• Iterate over each string element in the list test_list using a for loop.
• Check if the string check_str is present in the current string element of test_list using the in operator.
• If the check_str is present in the current string element, set the res variable to True and break out of the loop.
• Print the result of whether the check_str is part of any input list string by concatenating the string ‘Is check string part of any input list string : ‘ with the string representation of res.

## Python3

 `# initializing list` `test_list ``=` `[``'GeeksforGeeks'``, ``'is'``, ``'Best'``]`   `# test string` `check_str ``=` `"for"`   `# printing original string` `print``(``"The original string is : "` `+` `str``(test_list))`   `# Using loop` `# Check if substring is part of List of Strings` `res ``=` `False` `for` `string ``in` `test_list:` `    ``if` `check_str ``in` `string:` `        ``res ``=` `True` `        ``break`   `# 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```

Time complexity: O(n*m), where n is the number of strings in the list and m is the average length of each string.
Auxiliary space: O(1) since we are only using a few variables to store the results.

Previous
Next