Convert a list of strings to Lowercase in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Convert a list of strings to Lowercase in Python #

To convert a list of strings to lowercase:

  1. Use a list comprehension to iterate over the list.
  2. Use the str.lower() method to convert each string to lowercase.
  3. All strings in the new list will be lowercase.
main.py
list_of_strings = ['BOBBY', 'HADZ', 'COM'] # ✅ convert list of strings to lowercase (list comprehension) result = [item.lower() for item in list_of_strings] print(result) # 👉️ ['bobby', 'hadz', 'com'] # -------------------------------------------------- # ✅ convert list of strings to lowercase (map()) result = list(map(str.lower, list_of_strings)) print(result) # 👉️ ['bobby', 'hadz', 'com']

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.
main.py
list_of_strings = ['BOBBY', 'HADZ', 'COM'] result = [item.lower() for item in list_of_strings] print(result) # 👉️ ['bobby', 'hadz', 'com']

On each iteration, we use the str.lower() method to convert the current list item to lowercase and return the result.

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

main.py
my_str = 'BOBBYHADZ.COM' result = my_str.lower() print(result) # 👉️ bobbyhadz.com

The method does not change the original string, it returns a new string. Strings are immutable in Python.

Alternatively, you can use the map() function.

Convert a list of strings to Lowercase using map() #

To convert a list of strings to lowercase:

  1. Use the map() function to get a map object of lowercase strings.
  2. Use the list() class to convert the map object to a list.
  3. All strings in the new list will be lowercase.
main.py
list_of_strings = ['BOBBY', 'HADZ', 'COM'] result = list(map(str.lower, list_of_strings)) print(result) # 👉️ ['bobby', 'hadz', 'com']

We used the map() function to convert the strings in the list to lowercase.

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

The map() function calls the str.lower() method with each string in the list and returns the result.

The last step is to use the list() class to convert the map object to a list.

The list class takes an iterable and returns a list object.

Alternatively, you can use a simple for loop.

Convert a list of strings to Lowercase using for loop #

To convert a list of strings to lowercase:

  1. Use the enumerate() function to get access to the index of the current iteration.
  2. Use a for loop to iterate over the enumerate object.
  3. Use the str.lower() method to convert each item to lowercase.
main.py
list_of_strings = ['BOBBY', 'HADZ', 'COM'] for index, item in enumerate(list_of_strings): list_of_strings[index] = item.lower() print(list_of_strings) # 👉️ ['bobby', 'hadz', 'com']

We used the enumerate() function to iterate over the list with access to the current index.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
list_of_strings = ['BOBBY', 'HADZ', 'COM'] for index, item in enumerate(list_of_strings): print(index, item) # 👉️ 0 BOBBY, 1 HADZ, 2 COM

On each iteration, we reassign the current list item to its lowercase variant.

Which approach you pick is a matter of personal preference. I'd go with the list comprehension because I find it easier to read.

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.