Check if List contains a String case-insensitive in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Check if List contains a String case-insensitive in Python
  2. Check if List contains a String case-insensitive using casefold
  3. Check if List contains a String case-insensitive using map()

# Check if List contains a String case-insensitive in Python

To check if a list contains a string in a case-insensitive manner:

  1. Iterate over the list and convert each string to lowercase.
  2. Convert the string to check for to lowercase.
  3. Use the in operator to check if the string is in the list in a case-insensitive manner.
main.py
my_str = 'bobby' my_list = ['BOBBY', 'HADZ', 'COM'] if my_str.lower() in (item.lower() for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string is in list') else: print('The string is not in the list')

check if list contains string case insensitive

The code for this article is available on GitHub

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 use the str.lower() method to convert the current string to lowercase.

main.py
my_list = ['BOBBY', 'HADZ', 'COM'] # ๐Ÿ‘‡๏ธ ['bobby', 'hadz', 'com'] print(list(item.lower() for item in my_list))

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.

The last step is to convert the string we are checking for to lowercase and use the in operator.

main.py
my_str = 'bobby' my_list = ['BOBBY', 'HADZ', 'COM'] if my_str.lower() in (item.lower() for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string is in list') else: print('The string is not in the list')

The in operator tests for membership.

main.py
my_list = ['BOBBY', 'HADZ', 'COM'] print('BOBBY' in my_list) # ๐Ÿ‘‰๏ธ True print('another' in my_list) # ๐Ÿ‘‰๏ธ False

For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

Both strings have to either be lowercase or uppercase to perform a case-insensitive comparison.

# Handling non-string items

If your list contains items that aren't strings, use the str() class to convert each item to a string before calling the lower() method.

main.py
my_str = 'bobby' my_list = ['BOBBY', 1, 'HADZ', 2, 'COM'] if my_str.lower() in (str(item).lower() for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string is in the list') else: print('The string is not in the list')

handling non string items

The code for this article is available on GitHub

The lower() method is specific to strings, so we have to convert each list item to a string before using it.

# Check if List contains a String case-insensitive using casefold

You can also use the str.casefold() method instead of str.lower().

main.py
my_str = 'bobby' my_list = ['BOBBY', 'HADZ', 'COM'] if my_str.casefold() in (item.casefold() for item in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string is in list') else: print('The string is not in the list')

check if list contains string case insensitive using casefold

The code for this article is available on GitHub

Casefolding is similar to lowercasing but is more aggressive because it is intended to remove all case distinctions in a string.

Notice how the German lowercase letter รŸ is equal to ss.

main.py
# ๐Ÿ‘‡๏ธ using str.casefold() print('BOBBY'.casefold()) # ๐Ÿ‘‰๏ธ bobby print('รŸ'.casefold()) # ๐Ÿ‘‰๏ธ ss # ๐Ÿ‘‡๏ธ using str.lower() print('BOBBY'.lower()) # ๐Ÿ‘‰๏ธ bobby print('รŸ'.lower()) # ๐Ÿ‘‰๏ธ รŸ

Case folding is similar to lowercasing but is more aggressive because it is intended to remove all case distinctions in a string.

Notice how the German lowercase letter รŸ is equal to ss.

Since the letter is already lowercase, the str.lower() method returns the letter as is, while the str.casefold() method converts it to ss.

Using the str.casefold() method is not necessary if you are only comparing ASCII strings. In this case, using str.lower() is sufficient.

# Check if List contains a String case-insensitive using map()

You can also use the map() function to check if a list contains a string in a case-insensitive manner.

main.py
my_str = 'bobby' my_list = ['BOBBY', 'HADZ', 'COM'] if my_str.lower() in map(str.lower, my_list): # ๐Ÿ‘‡๏ธ this runs print('The string is in the list.') else: print('The string is not in the list.')
The code for this article is available on GitHub

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the str.lower method to the map() function, so it will convert each string in the list to lowercase.

The last step is to convert the string we are checking for to lowercase and use the in operator.

# 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