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 shortest and a readable way to check if a suffix exists in a list of strings, — use any ().
# Python code to check # suffix exists in list of strings. # Initialize the input list lst = [ "Paras" , "Geeksforgeeks" , "Game" ] # using any to find the suffix Output = any ( ’Geek’ in x for x in lst) # Print print ( "Initial List is: " , lst) print (Output ) |
Exit:
Initial List is: [’Paras’,’ Geeksforgeeks’, ’Game’] True
Method # 2: Using filter ()
and lambda
This is another way to accomplish this particular task using lambda ().
# Python code to check # suffix exists in the list lines. # Initialize the input list lst = [ "Paras" , "Geeksforgeeks" , "Game" ] # Using filter and lambda Output = len ( list ( filter ( lambda x : "Jai" in x, lst)))! = 0 # Printout print ( "Initial List is:" , lst) print (Output) |
Exit:
Initial List is : [’Paras’,’ Geeksforgeeks’, ’Game’] False
Python | Check if the suffix matches any string in the given list Python functions: Questions
Python | Check if the suffix matches any string in the given list String Variables: Questions