
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Check if suffix matches with any string in given list?
Many times we need to analyze if a given word is present in a given list. That helps us in further processing the business logic for the data. In this article we see how to find if a given suffix which is a string is present in a list with many strings.
Using any
The any() function in python returns True if an item is present in an iterable. If nto it will return False. So in the below program we design if clauses to check for the presence or absence of the given string in the list.
Example
# Given List lstA = ["Tue", "Fri", "Sun"] # String to check str = 'Sun' # use any if any(str in i for i in lstA): print(str,"is present in given list.") else: print(str, "is absent from the given list.")
Output
Running the above code gives us the following result −
Sun is present in given list.
Use filter
In the below program we see how to use filter function. This function returns an iterator when the items are filtered through a function to test if the item is accepted or not. Then we convert the result of filter function to a list and check the length of the list. If the length is greater than zero then we say the string is present else it is absent.
Example
# Given List lstA = ["Tue", "Fri", "Sun"] # String to check str = 'Wed' # use filter if len(list(filter(lambda x: str in x, lstA))) != 0: print(str,"is present in given list.") else: print(str, "is absent from the given list.")
Output
Running the above code gives us the following result −
Wed is absent from the given list.
- Related Articles
- How to check if string or a substring of string ends with suffix in Python?
- Check if a string is suffix of another in Python
- Check if suffix and prefix of a string are palindromes in Python
- How to check if a string ends with a specified Suffix string in Golang?
- Check if a String starts with any of the given prefixes in Java
- Java Program to check if any String in the list starts with a letter
- Check if given string can be formed by concatenating string elements of list in Python
- Java Program to check if none of the string in the list matches the condition
- Python – Check if any list element is present in Tuple
- Check if every List element matches the predicate conditions in C#
- Python program to delete suffix from the given string
- Check if a list exists in given list of lists in Python
- Program to check if a string contains any special character in Python
- Check if a string starts with given word in PHP
- Check if a string ends with given word in PHP
