Python | Check if suffix matches with any string in given list
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 initialization lst = ["Paras", "Geeksforgeeks", "Game"] # using any to find suffix Output = any('Geek' in x for x in lst) # Printing output print("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 initialization lst = ["Paras", "Geeksforgeeks", "Game"] # Using filter and lambda Output = len(list(filter(lambda x: "Jai" in x, lst))) != 0 # Printing output print("Initial List is :", lst) print(Output) |
Initial List is : ['Paras', 'Geeksforgeeks', 'Game'] False
Recommended Posts:
- Python | Find all close matches of input string from a list
- Python | Check Numeric Suffix in String
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Segregate list elements by Suffix
- Python | Check if a list exists in given list of lists
- Python | Convert list of string to list of list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert a string representation of list into list
- Python | Convert string enclosed list to list
- Python | Check if a list is contained in another list
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if a given string is binary string or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



