Open In App

Python - Convert Strings to Uppercase in Dictionary Value Lists

Last Updated : 22 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, sometimes a dictionary contains lists as its values, and we want to convert all the string elements in these lists to uppercase. For example, consider the dictionary {'a': ['hello', 'world'], 'b': ['python', 'programming']}. We want to transform it into {'a': ['HELLO', 'WORLD'], 'b': ['PYTHON', 'PROGRAMMING']} by converting all strings in the value lists to uppercase. Let's discuss various methods to achieve this.

Using Dictionary Comprehension with List Comprehension

This method uses a dictionary comprehension combined with list comprehension to iterate through the dictionary and convert strings in value lists to uppercase.

Python
# Input dictionary
d = {'a': ['hello', 'world'], 'b': ['python', 'programming']}

# Convert strings to uppercase using dictionary and list comprehensions
res = {k: [v.upper() for v in vals] for k, vals in d.items()}

print(res)

Output
{'a': ['HELLO', 'WORLD'], 'b': ['PYTHON', 'PROGRAMMING']}

Explanation:

  • The dictionary comprehension iterates over each key-value pair in the dictionary.
  • For each value list, a list comprehension is used to iterate through its elements.
  • Each string element is converted to uppercase using the upper method.

Let's explore some more ways and see how we convert strings to uppercase in Dictionary value lists.

Using For Loop with List Comprehension

This method uses a regular for loop for clarity.

Python
# Input dictionary
d = {'a': ['hello', 'world'], 'b': ['python', 'programming']}

# Initialize an empty dictionary to store the result
res = {}

# Iterate through the dictionary
for k, vals in d.items():
    # Convert each string in the value list to uppercase
    res[k] = [v.upper() for v in vals]

print(res)

Output
{'a': ['HELLO', 'WORLD'], 'b': ['PYTHON', 'PROGRAMMING']}

Explanation:

  • The method uses a regular for loop to iterate through each key-value pair.
  • For each value list, a list comprehension is used to process and convert strings to uppercase.
  • The resulting key-value pair is stored in a new dictionary.

Using map()

This method uses the map() function for transforming elements in the value lists to uppercase.

Python
# Input dictionary
d = {'a': ['hello', 'world'], 'b': ['python', 'programming']}

# Convert strings to uppercase using map
res = {k: list(map(str.upper, vals)) for k, vals in d.items()}

print(res)

Output
{'a': ['HELLO', 'WORLD'], 'b': ['PYTHON', 'PROGRAMMING']}

Explanation:

  • map() function applies the str.upper method to each element in the value list.
  • list function converts the result of map back to a list.
  • Dictionary comprehension iterates through each key-value pair and applies the transformation.

Using Nested Loops

This method uses nested loops to explicitly iterate over the dictionary and the value lists.

Python
# Input dictionary
d = {'a': ['hello', 'world'], 'b': ['python', 'programming']}

# Initialize an empty dictionary to store the result
res = {}

# Outer loop for the dictionary
for k, vals in d.items():
    # Inner loop to process each string in the value list
    res[k] = []
    for v in vals:
        res[k].append(v.upper())

print(res)

Output
{'a': ['HELLO', 'WORLD'], 'b': ['PYTHON', 'PROGRAMMING']}

Explanation:

  • Outer loop iterates through each key-value pair in the dictionary.
  • Inner loop iterates through the elements of each value list and converts them to uppercase.
  • The processed list is stored in the resulting dictionary.

Next Article

Similar Reads