Open In App

Python – Convert Strings to Uppercase in Dictionary value lists

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given dictionary with values list, convert all the strings to upper case.

Input : {“Gfg” : [“ab”, “cd”], “Best” : [“gh”], “is” : [“kl”]} 
Output : {‘Gfg’: [‘AB’, ‘CD’], ‘Best’: [‘GH’], ‘is’: [‘KL’]} 
Explanation : All value lists strings are converted to upper case. 

Input : {“Gfg” : [“ab”, “cd”, “Ef”]} 
Output : {‘Gfg’: [‘AB’, ‘CD’, “EF”]} 
Explanation : All value lists strings are converted to upper case, already upper case have no effect.

Method #1 : Using dictionary comprehension + upper() + list comprehension

The combination of above functions can be used to solve this problem. In this, we perform upper case using upper(), list comprehension is used to iterate through all strings, dictionary comprehension is used to remake dictionary with upper case values.

Python3




# Python3 code to demonstrate working of
# Convert Strings to Uppercase in Dictionary value lists
# Using dictionary comprehension + upper() + list comprehension
 
# initializing dictionary
test_dict = {"Gfg" : ["ab", "cd", "ef"],
             "Best" : ["gh", "ij"], "is" : ["kl"]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using upper to convert to upper case
res = {key: [ele.upper() for ele in test_dict[key] ] for key in test_dict }
 
# printing result
print("The dictionary after conversion " + str(res))


Output

The original dictionary is : {‘Gfg’: [‘ab’, ‘cd’, ‘ef’], ‘Best’: [‘gh’, ‘ij’], ‘is’: [‘kl’]} The dictionary after conversion {‘Gfg’: [‘AB’, ‘CD’, ‘EF’], ‘Best’: [‘GH’, ‘IJ’], ‘is’: [‘KL’]}

Time complexity: O(nm), where n is the number of keys in the dictionary and m is the maximum length of the list associated with any key.
Auxiliary Space: O(nm), where n is the number of keys in the dictionary and m is the maximum length of the list associated with any key. This is because we create a new dictionary with the same number of keys and lists of the same length as the original dictionary, and then we modify each list by creating a new list with the same number of elements but with each element converted to uppercase.

Method #2 : Using map() + upper() + dictionary comprehension

The combination of above functions can be used to solve this problem. In this, we perform the task of extending logic of upper case using map() instead of list comprehension.

Step by step approach :

  1. First, a dictionary named test_dict is initialized with some keys and their respective values as lists of strings.
  2. The original dictionary is printed using the print() function and str() conversion of test_dict.
  3. A dictionary comprehension is used to create a new dictionary res with the same keys as test_dict, but with each value list converted to all uppercase using the map() and str.upper() functions.
  4. The result dictionary res is printed using the print() function and str() conversion.

Python3




# Python3 code to demonstrate working of
# Convert Strings to Uppercase in Dictionary value lists
# Using map() + upper() + dictionary comprehension
 
# initializing dictionary
test_dict = {"Gfg" : ["ab", "cd", "ef"],
             "Best" : ["gh", "ij"], "is" : ["kl"]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using map() to extend logic to all inner list
res = {key: list(map(str.upper, test_dict[key])) for key in test_dict}
 
# printing result
print("The dictionary after conversion " + str(res))


Output

The original dictionary is : {‘Gfg’: [‘ab’, ‘cd’, ‘ef’], ‘Best’: [‘gh’, ‘ij’], ‘is’: [‘kl’]} The dictionary after conversion {‘Gfg’: [‘AB’, ‘CD’, ‘EF’], ‘Best’: [‘GH’, ‘IJ’], ‘is’: [‘KL’]}

Time complexity: O(N*M), where N is the number of keys in the dictionary and M is the maximum length of the list associated with any key
Auxiliary space: O(N*M), where N is the number of keys in the dictionary and M is the maximum length of the list associated with any key.

Method 3: using a combination of dictionary comprehension and the built-in function zip() to iterate over both keys and values simultaneously:

Python3




# initializing dictionary
test_dict = {"Gfg": ["ab", "cd", "ef"],
             "Best": ["gh", "ij"], "is": ["kl"]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using dictionary comprehension and zip to iterate over keys and values simultaneously
res = {k: [s.upper() for s in v]
       for k, v in zip(test_dict.keys(), test_dict.values())}
 
# printing result
print("The dictionary after conversion: " + str(res))


Output

The original dictionary is : {'Gfg': ['ab', 'cd', 'ef'], 'Best': ['gh', 'ij'], 'is': ['kl']}
The dictionary after conversion: {'Gfg': ['AB', 'CD', 'EF'], 'Best': ['GH', 'IJ'], 'is': ['KL']}

Time complexity:  O(N*M), where N is the number of keys in the dictionary and M is the length of the longest list in the dictionary.
Auxiliary space: O(N*M), as it creates a new dictionary with the same number of keys as the original dictionary, and each value in the new dictionary is a list with the same number of elements as the corresponding value in the original dictionary.

Method #4: Using a for loop to iterate over the dictionary keys and values, and then using a nested for loop to iterate over the list values and convert them to uppercase using the upper() method.

Steps:

  1. Define a dictionary test_dict with three keys and values, where the values are lists of strings.
  2. Print the original dictionary using print() function and str() function for converting dictionary into string.
  3. Use a for loop with items() method to iterate over the keys and values of the dictionary test_dict.
  4. Within the for loop, use a nested for loop with range() function and len() function to iterate over the list values of the dictionary.
  5. Within the nested for loop, use upper() method to convert the strings to uppercase.
  6. Assign the converted list back to the original dictionary using the key and index.
  7. Print the resulting dictionary after the conversion of strings to uppercase using print() function and str() function for converting dictionary into string.

Python3




# Python3 code to demonstrate working of
# Convert Strings to Uppercase in Dictionary value lists
# Using for loop and upper() method
 
# initializing dictionary
test_dict = {"Gfg" : ["ab", "cd", "ef"],
             "Best" : ["gh", "ij"], "is" : ["kl"]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using a for loop to iterate over the dictionary keys and values
for key, value in test_dict.items():
    # using a nested for loop to iterate over the list values and convert them to uppercase
    for i in range(len(value)):
        value[i] = value[i].upper()
 
# printing result
print("The dictionary after conversion " + str(test_dict))


Output

The original dictionary is : {'Gfg': ['ab', 'cd', 'ef'], 'Best': ['gh', 'ij'], 'is': ['kl']}
The dictionary after conversion {'Gfg': ['AB', 'CD', 'EF'], 'Best': ['GH', 'IJ'], 'is': ['KL']}

Time complexity: O(NM), where N is the number of keys in the dictionary and M is the maximum number of values in any key’s list.
Auxiliary space: O(1) because we are modifying the original dictionary in place and not creating any new data structures. 

Method #5: Using a list comprehension inside a dictionary comprehension to convert the values to uppercase in a single line of code:

This method uses a nested comprehension where the inner list comprehension iterates over the values of the dictionary and converts them to uppercase. The outer dictionary comprehension iterates over the key-value pairs of the original dictionary and creates a new dictionary with the same keys but with the values converted to uppercase.

Python3




test_dict = {"Gfg" : ["ab", "cd", "ef"],
             "Best" : ["gh", "ij"], "is" : ["kl"]}
 
# using dictionary comprehension with list comprehension
test_dict = {key: [val.upper() for val in value] for key, value in test_dict.items()}
 
# printing result
print("The dictionary after conversion " + str(test_dict))


Output

The dictionary after conversion {'Gfg': ['AB', 'CD', 'EF'], 'Best': ['GH', 'IJ'], 'is': ['KL']}

Time complexity: O(n*m), where n is the number of keys in the dictionary and m is the maximum length of the value list.
Auxiliary space: O(n*m), as the new dictionary has the same number of keys as the original dictionary, and the maximum size of any value list in the new dictionary is the same as in the original dictionary.

Method #6: Using the dictionary’s items() method to iterate over the key-value pairs of the dictionary, and then using a for loop to iterate over the list values and convert them to uppercase using the upper() method.

Step-by-Step approach:

  1. Initialize the dictionary test_dict with the given values.
  2. Create an empty dictionary res to store the converted values.
  3. Iterate over the key-value pairs in the test_dict using the items() method.
  4. For each key-value pair, iterate over the list values using a for loop.
  5. Convert each value to uppercase using the upper() method.
  6. Add the key-value pair to the res dictionary with the converted values.
  7. Print the original and converted dictionaries.

Python3




# initializing dictionary
test_dict = {"Gfg": ["ab", "cd", "ef"], "Best": ["gh", "ij"], "is": ["kl"]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using items() method and for loop to convert to upper case
res = {}
for key, value in test_dict.items():
    res[key] = [ele.upper() for ele in value]
 
# printing result
print("The dictionary after conversion " + str(res))


Output

The original dictionary is : {'Gfg': ['ab', 'cd', 'ef'], 'Best': ['gh', 'ij'], 'is': ['kl']}
The dictionary after conversion {'Gfg': ['AB', 'CD', 'EF'], 'Best': ['GH', 'IJ'], 'is': ['KL']}

Time complexity: O(NM), where N is the number of keys in the dictionary and M is the average length of the value lists.
Auxiliary space: O(NM), as we are creating a new dictionary with converted values.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads