VOOZH about

URL: https://www.geeksforgeeks.org/python/python-convert-strings-to-uppercase-in-dictionary-value-lists/

⇱ Python - Convert Strings to Uppercase in Dictionary Value Lists - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Convert Strings to Uppercase in Dictionary Value Lists

Last Updated : 15 Jul, 2025

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.


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.


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.


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.


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.
Comment