Check if any element in list starts with a string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 10, 2022

banner

Photo from Unsplash

Check if any element in list starts with a string in Python #

Use the any() function to check if any element in a list starts with a specific string, e.g. if any(item.startswith('Ki') for item in my_list):. The any function will return True if any element in the list starts with the string and False otherwise.

main.py
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any(item.startswith('Ki') for item in my_list): # 👇️ this runs print('There is a list item that starts with Ki') else: print('No list items start with Ki') # 👇️ True print(any(item.startswith('Ki') for item in my_list))

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

We passed a generator expression to the any() function.

Generator expressions are used to perform some operation for every element, or select a subset of elements that meet a condition.

In the example, we check if each item in the list starts with the string Ki and return the result.

If the condition is met at least once, the any() function returns True.

You can use the assignment expression syntax if you need to get the element that starts with the specified string.

main.py
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any((match := item).startswith('Ki') for item in my_list): # 👇️ this runs print('There is a list item that starts with Ki') print(match) # 👉️ Kiwi else: print('No list items start with Ki')

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

The str.startswith method returns True if the string starts with the provided prefix, otherwise the method returns False.

Note that the str.startswith() method performs a case-sensitive comparison.

If you need to check if any element in a list starts with a string ignoring its case, convert both strings to lowercase.

main.py
my_list = ['Apple', 'Banana', 'Kiwi', 'Melon'] if any(item.lower().startswith('KI'.lower()) for item in my_list): # 👇️ this runs print('There is a list item that starts with Ki') else: print('No list items start with Ki') # 👇️ True print(any(item.lower().startswith('KI'.lower()) for item in my_list))
We used the str.lower() method to convert each list item and the string we are checking for to lowercase.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

If the iterable we pass to the any() function is empty or none of the elements in the iterable are truthy, the any function returns False.

main.py
my_list = ['Apple', 'Banana'] if any(item.startswith('Ki') for item in my_list): print('There is a list item that starts with Ki') else: # 👇️ this runs print('No list items start with Ki') # 👇️ False print(any(item.startswith('Ki') for item in my_list))

None of the items in the list starts with Ki, so the condition is never met and any() returns False.

If your list is empty, the any function will always return False.

main.py
my_list = [] if any(item.startswith('Ki') for item in my_list): print('There is a list item that starts with Ki') else: # 👇️ this runs print('No list items start with Ki') # 👇️ False print(any(item.startswith('Ki') for item in my_list))
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.