Coding Challenge π₯
π¬ Question: How to convert a list of strings to all uppercase in Python?
Here are three examples:
['hello', 'Python', 'world']
to['HELLO', 'PYTHON', 'WORLD']
['a', 'b', 'c']
to['A', 'B', 'C']
['aa aa', 'bb$bb', 'cc()cc']
to['AA AA', 'BB$BB', 'CC()CC']
There are multiple great ways to accomplish this coding challenge and I strongly believe it’s a great learning tool to dive into each of them. So, let’s get started with my most recommended solution first! π
Method 1: List Comprehension + str.upper() π
The most Pythonic way to convert a string list my_list
to all uppercase strings is to use the str.upper()
method inside a list comprehension expression like so: [x.upper() for x in my_list]
. The result is a new string list, all uppercase.
Here’s this method applied to our example strings:
def all_upper(my_list): return [x.upper() for x in my_list] print(all_upper(['hello', 'Python', 'world'])) # ['HELLO', 'PYTHON', 'WORLD'] print(all_upper(['a', 'b', 'c'])) # ['A', 'B', 'C'] print(all_upper(['aa aa', 'bb$bb', 'cc()cc'])) # ['AA AA', 'BB$BB', 'CC()CC']
If you struggle with list comprehension, I’d highly urge you to watch the following explainer video and dive into the related tutorial:
π Recommended Tutorial: List Comprehension in Python — A Helpfull Illustrated Guide
If you want to convert a string list to all lowercase, check out this detailed tutorial on the Finxter blog.
Method 2: Map Function + String Upperπ
A concise Python one-liner to convert a string list to all uppercase strings is to use the map()
function with a lambda function as a first argument — converting a string argument x
to its uppercase variant using x.upper()
— and the list of strings as the second argument. The result is a map object that you can convert to a list using the list()
built-in function.
Here’s an example on the same sample lists:
def all_upper(my_list): return list(map(lambda x: x.upper(), my_list)) print(all_upper(['hello', 'Python', 'world'])) # ['HELLO', 'PYTHON', 'WORLD'] print(all_upper(['a', 'b', 'c'])) # ['A', 'B', 'C'] print(all_upper(['aa aa', 'bb$bb', 'cc()cc'])) # ['AA AA', 'BB$BB', 'CC()CC']
An even more concise variant of this is to pass the str.upper()
method object right as a first argument of the map()
function like so:
def all_upper(my_list): return list(map(str.upper, my_list))
If you struggle with the map()
function, I’d recommend you check out the following video and blog tutorial:
π Recommended Tutorial: Python Map Function
Method 3: Simple For Loop and List Append π
The default but not so idiomatic way to convert a string list to an uppercase string list is to create an empty list and iterate over all elements in the original list, converting each element to a lowercase string using x.upper()
and appending this element to the newly-created list.
Here’s a minimal example:
def all_upper(my_list): new_list = [] for x in my_list: new_list.append(x.upper()) return new_list print(all_upper(['hello', 'Python', 'world'])) # ['HELLO', 'PYTHON', 'WORLD'] print(all_upper(['a', 'b', 'c'])) # ['A', 'B', 'C'] print(all_upper(['aa aa', 'bb$bb', 'cc()cc'])) # ['AA AA', 'BB$BB', 'CC()CC']
It’s not so bad—mainly due to the efficiency of the list.append()
method. Feel free to check out our full guide on list.append()
with many interesting factoids such as which is faster: list.append()
or list.extend()
?
π Recommended Tutorial: Python List Append Method
Summary
You’ve learned three main ways to convert a list of strings to a list of uppercase strings:
[x.upper() for x in my_list]
list(map(str.upper, my_list))
- Loop over all
x
and calllist.append(x.upper())
In the process of solving this problem, we’ve learned many new Python features that every Python pro coder should know without using Google. π
If you want to keep improving your skills, I’ll invite you to join our free email academy and download our Python cheat sheets here: