Last updated: Apr 9, 2024
Reading timeยท3 min
To check if a list contains a string in a case-insensitive manner:
in
operator to check if the string is in the list in a
case-insensitive manner.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')
We used a generator expression to iterate over the list.
On each iteration, we use the str.lower()
method to convert the current string
to lowercase.
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.
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.
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
.
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.
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')
The lower()
method is specific to strings, so we have to convert each list
item to a string before using it.
You can also use the str.casefold()
method instead of str.lower()
.
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')
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
.
# ๐๏ธ 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
.
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.
You can also use the map()
function to check if a list contains a string in a
case-insensitive manner.
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 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.
You can learn more about the related topics by checking out the following tutorials: