Given a list of strings, the task is to check whether suffix matches with any string in the given list.
Examples:
Input: lst = ["Paras", "Geeksforgeeks", "Game"], str = 'Geeks' Output: True Input: lst = ["Geeks", "for", "forgeeks"], str = 'John' Output: False
Let’s discuss a few methods to do the task.
Method #1 : Using any()
The most concise and readable way to check whether suffix exists in list of strings is to use any().
# Python code to check whether# suffix exists in list of strings. # Input list initializationlst = ["Paras", "Geeksforgeeks", "Game"] # using any to find suffixOutput = any('Geek' in x for x in lst) # Printing outputprint("Initial List is :", lst)print(Output) |
Initial List is : ['Paras', 'Geeksforgeeks', 'Game'] True
Method #2 : Using filter() and lambda
This is yet another way to perform this particular task using lambda().
# Python code to check whether# suffix exists in list of strings. # Input list initializationlst = ["Paras", "Geeksforgeeks", "Game"] # Using filter and lambdaOutput = len(list(filter(lambda x: "Jai" in x, lst))) != 0 # Printing outputprint("Initial List is :", lst)print(Output) |
Initial List is : ['Paras', 'Geeksforgeeks', 'Game'] False
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


