Explain Codes LogoExplain Codes Logo

Convert a list with strings all to lowercase or uppercase

python
list-comprehensions
map-function
lambda-functions
Anton ShumikhinbyAnton Shumikhin·Nov 8, 2024
TLDR

Convert a list of strings to all lowercase or uppercase with a one-liner:

Lowercase:

lowercase_list = [s.lower() for s in my_list] # Joke: strings screaming "I can't hear you" in a quiet library

Uppercase:

uppercase_list = [s.upper() for s in my_list] # Joke: strings shouting at a rock concert

Just swap my_list with your actual list of strings.

No-Nonsense Conversion: List comprehensions and map

Let's wield the power of Python's list comprehensions or go old school using map() and lambda:

# Lowercase using map + lambda: When strings forgot their caps at home lowercase_list_map = list(map(lambda s: s.lower(), my_list)) # Uppercase using map + str.upper: Strings make it loud and proud! uppercase_list_map = list(map(str.upper, my_list))

Each new list contains all strings from the original list but dressed in a consistent case, leaving your original data untouched.

Eggs in Different Baskets: Various Situations

All methods are great but choosing the right one depends on your situation. Here are some Pythonic solutions to consider:

1. Map vs. List Comprehension

Map offers lazy evaluation, while list comprehensions quickly whip up an entire list. For complex conditions and expressions, list comprehensions might be a better bet.

2. Loving the Original

Want to preserve the original list? Create a copy before turning up or down the voice of strings:

original_list = ["Python", "LiSt", "LoWEr", "CASE", "UPPER"] # Work on a copy, original list calls out "Don't touch me" copied_list = original_list[:] uppercase_copied = [s.upper() for s in copied_list]

3. A Tortoise vs. the Hare

For larger datasets, consider memory and speed. Generators or map() function are more memory-friendly solutions thanks to their on-the-fly value generation:

# A generator: It's not about the speed, it's the memory lowercase_generator = (s.lower() for s in my_list) # Consume the generator as you iterate...Yum...Delicious! for word in lowercase_generator: print(word)

Tricks and Traps: Handling Edge Cases

Now, let's dive in to tackle some unusual yet important situations:

Unicode Strings and Locales

For Unicode strings in different languages with complex character cases, you might want to call some external help:

import unicodedata # Unicode-aware lowercase conversion, because globalization... lowercase_unicode = [unicodedata.normalize('NFKD', s).encode('ASCII', 'ignore').decode().lower() for s in my_list]

Filtering Non-String Villains

If your list houses non-string elements, keep them out of the transformation process:

# Only change the strings... numbers, stay where you are! uniform_case_list = [s.lower() if isinstance(s, str) else s for s in my_list]

In-Place Modification: A Risky Move

Finally, if you don't mind changing the original list:

# A dangerous game... use if you dare... for i, s in enumerate(my_list): if isinstance(s, str): my_list[i] = s.lower()

Remember, it's a risky move that permanently changes the original list.