Convert a List to Lowercase in Python
To convert a Python list containing strings of chars with mixed casing use either the str.lower()
function in a for loop, the map()
function or list comprehension.
Convert all List Strings to Lowercase with str.lower()
The str.lower()
function looks for uppercase characters in a string and replaces them with their lowercase counterpart. For lists of strings, we can iterate through the list and replace each string with a lowercase version.
items = ['SoMe tExT', 'moRE TEXT']
for i in range(len(items)):
items[i] = items[i].lower()
print(items)
['some text', 'more text']
Use the map() Function to Change a List of Strings to Lowercase
We can use the Python map()
function with a lambda function to turn all strings in a list to lowercase.
items = ['SoMe tExT', 'moRE TEXT']
lower = (map(lambda x: x.lower(), items))
lower_list = list(lower)
print(lower_list)
['some text', 'more text']
This method is nice because it is more compact than the for loop solution. The downside is that it is less readable and the result needs to be converted back into a list.
Convert List to Lowercase with List Comprehension
List comprehension allows us to create a list on one line using a lambda function to perform the lowercase conversion.
items = ['SoMe tExT', 'moRE TEXT']
lower = [x.lower() for x in items]
print(lower)
['some text', 'more text']