Check if a Letter is in a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if a Letter is in a List in Python #

To check if a letter is in a list:

  1. Use a generator expression to iterate over the list.
  2. Use the in operator to check if the letter is contained in each list item.
  3. If the condition is met at least once, the letter is contained in the list.
main.py
my_list = ['bobby', 'hadz', 'com'] letter = 'z' result = any(letter in word for word in my_list) print(result) # 👉️ True if any(letter in word for word in my_list): # 👇️ this runs print('The letter is contained in at least one of the list items') else: print('The letter is NOT contained in any of the list items') # --------------------------------------------------- # ✅ find the list items in which the letter is contained matches = [word for word in my_list if letter in word] print(matches) # 👉️ ['hadz']

We used a generator expression to iterate over the list.

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

On each iteration, we check if the letter is contained in the current list item and return the result.

main.py
my_list = ['bobby', 'hadz', 'com'] letter = 'z' result = any(letter in word for word in my_list) print(result) # 👉️ True

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
my_str = 'bobby hadz' print('bobby' in my_str) # 👉️ True print('another' in my_str) # 👉️ False
x not in s returns the negation of x in s.

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

If you need to check if a certain letter is contained in any of the items in a list ignoring the case, convert both strings to lowercase.

main.py
my_list = ['BOBBY', 'HADZ', 'COM'] letter = 'z' result = any(letter.lower() in word.lower() for word in my_list) print(result) # 👉️ True if any(letter.lower() in word.lower() for word in my_list): # 👇️ this runs print('The letter is contained in at least one of the list items') else: print('The letter is NOT contained in any of the list items')

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

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

We can perform a case-insensitive membership test by converting both strings to lowercase or uppercase.

If you need to find the list items in which the letter is contained, use a list comprehension.

main.py
my_list = ['bobby', 'hadz', 'com'] letter = 'z' matches = [word for word in my_list if letter in word] print(matches) # 👉️ ['hadz']
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The new list contains only the list items in which the letter is contained.

If you need to perform a case-insensitive membership test, convert both strings to lowercase.

main.py
my_list = ['bobby', 'hadz', 'com'] letter = 'Z' matches = [word for word in my_list if letter.lower() in word.lower()] print(matches) # 👉️ ['hadz']
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.