Check if String starts with any Element from List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
7 min

banner

# Table of Contents

  1. Check if a String starts with any element from List in Python
  2. Check if any element in a List starts with a string in Python
  3. Find list items starting with a given string in Python

# Check if a String starts with any element from List in Python

To check if a string starts with any element from a list:

  1. Use the tuple() class to convert the list to a tuple.
  2. Use the startswith() method to check if the string starts with any element in the tuple.
  3. The startswith method will return True if the condition is met.
main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] # โœ… Using str.startswith() with a tuple if my_str.startswith(tuple(my_list)): # ๐Ÿ‘‡๏ธ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # ๐Ÿ‘‡๏ธ True print(my_str.startswith(tuple(my_list)))
The code for this article is available on GitHub

The first example uses the str.startswith() method.

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

The startswith method can be passed a string or a tuple of strings.

If you have a list, make sure to convert it to a tuple by passing it to the tuple() class.

The startswith method will return True if the string starts with any of the strings in the tuple, otherwise False is returned.

# Check if a string starts with any element from List using any()

Alternatively, you can use the any() function.

The any function will return True if the string starts with any element from the list, otherwise False is returned.

main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] if any(my_str.startswith(item) for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # ๐Ÿ‘‡๏ธ True print(any(my_str.startswith(item) for item in my_list))
The code for this article is available on GitHub

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 the string starts with each item from the list and return the result.

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

# Getting the list item that meets the condition

You can use the assignment expression syntax if you need to get the list item the string starts with.

main.py
my_str = 'hello' my_list = ['apple', 'one', 'he'] if any(my_str.startswith(match := item) for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('string starts with at least one of the elements from the list') print(match) # ๐Ÿ‘‰๏ธ 'he' else: print('string does NOT start with any of the elements from the list')
The code for this article is available on GitHub

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

# Checking if a String starts with any element from List, ignoring the case

If you need to check if a string starts with any element from a list, in a case-insensitive manner, convert both strings to lowercase.

main.py
my_str = 'HELLO' my_list = ['apple', 'one', 'he'] if any(my_str.lower().startswith(item.lower()) for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('string starts with at least one of the elements from the list') else: print('string does NOT start with any of the elements from the list') # ๐Ÿ‘‡๏ธ True print(any(my_str.lower().startswith(item.lower()) for item in my_list))
The code for this article is available on GitHub
We used the str.lower() method to convert the string and each list item to lowercase before calling the startswith method.

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_str = 'hello' my_list = ['apple', 'one'] if any(my_str.startswith(item) for item in my_list): print('string starts with at least one of the elements from the list') else: # ๐Ÿ‘‡๏ธ this runs print('string does NOT start with any of the elements from the list') # ๐Ÿ‘‡๏ธ False print(any(my_str.startswith(item) for item in my_list))

The string doesn't start with any of the items from the list, 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_str = 'hello' my_list = [] if any(my_str.startswith(item) for item in my_list): print('string starts with at least one of the elements from the list') else: # ๐Ÿ‘‡๏ธ this runs print('string does NOT start with any of the elements from the list') # ๐Ÿ‘‡๏ธ False print(any(my_str.startswith(item) for item in my_list))

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

Use the any() function to check if any element in a list starts with a specific string.

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))

check if any element in list starts with string

The code for this article is available on GitHub

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.

# Getting the list elements that starts with the string

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')

getting list elements that starts with the string

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.

# Check if any element in a List starts with a string, ignoring the case

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))
The code for this article is available on GitHub
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))

# Find list items starting with a given string in Python

To find the list items that start with a given string:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.startswith() method to check if each item starts with the string.
  3. The new list will only contain the items that meet the condition.
main.py
a_list = ['apple', 'application', 'example'] string = 'app' new_list = [item for item in a_list if item.startswith(string)] print(new_list) # ๐Ÿ‘‰๏ธ ['apple', 'application']
The code for this article is available on GitHub

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.startswith() method to check if the current item starts with the given string.

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

The new list only contains the items that meet the condition.

# Find list items starting with a given string, ignoring the case

If you need to find the list items that start with a given string in a case-insensitive manner, use the str.lower() method.

main.py
a_list = ['apple', 'application', 'example'] string = 'APP' new_list = [item for item in a_list if item.lower().startswith(string.lower())] print(new_list) # ๐Ÿ‘‰๏ธ ['apple', 'application']

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

By converting both of the strings to lowercase, we check if the string starts with the substring in a case-insensitive manner.

Alternatively, you can use a for loop.

# Find list items starting with a given string using a for loop

This is a three-step process:

  1. Use a for loop to iterate over the list.
  2. Use the str.startswith() method to check if each item starts with the given string.
  3. Append the matching items to a new list.
main.py
a_list = ['apple', 'application', 'example'] string = 'app' new_list = [] for item in a_list: if item.startswith(string): new_list.append(item) print(new_list) # ๐Ÿ‘‰๏ธ ['apple', 'application']
The code for this article is available on GitHub

We used a for loop to iterate over the list.

On each iteration, we check if the current item starts with the specified string.

If the condition is met, we append the item to a new list.

The list.append() method adds an item to the end of the list.

If you only need to find the first list item that starts with the given string, use a break statement once you encounter a match.
main.py
a_list = ['apple', 'application', 'example'] string = 'app' match = None for item in a_list: if item.startswith(string): match = item break print(match) # ๐Ÿ‘‰๏ธ 'apple'

If we find an item that starts with the given string, we assign the value to a variable and break out of the loop.

The break statement breaks out of the innermost enclosing for or while loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev