Python Program To Convert dictionary values to Strings
Given dictionary with mixed data types as values, the task is to write a Python program to convert to parsed strings by different delims.
Examples:
Input : test_dict = {‘Gfg’ : 4, ‘is’ : “1”, ‘best’ : [8, 10], ‘geek’ : (10, 11, 2)}, list_delim, tuple_delim = ‘-‘, ‘^’
Output : {‘Gfg’: ‘4’, ‘is’: ‘1’, ‘best’: ‘8-10’, ‘geek’: ’10^11^2′}
Explanation : List elements are joined by -, tuples by ^ symbol.Input : test_dict = {‘Gfg’ : 4, ‘is’ : “1”, ‘best’ : [8, 10], ‘geek’ : (10, 11, 2)}, list_delim, tuple_delim = ‘*’, ‘,’
Output : {‘Gfg’: ‘4’, ‘is’: ‘1’, ‘best’: ‘8*10’, ‘geek’: ‘10,11,2’}
Explanation : List elements are joined by *, tuples by , symbol.
Example: Using loop + isinstance() + join()
In this, we check for all the values data types using isinstance() and join using join() for difference delims, converting to parsed strings.
Python3
# Python3 code to demonstrate working of # Convert dictionary values to Strings # Using loop + isinstance() # initializing dictionary test_dict = { 'Gfg' : 4 , 'is' : "1" , 'best' : [ 8 , 10 ], 'geek' : ( 10 , 11 , 2 )} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # initializing delims list_delim, tuple_delim = '-' , '^' res = dict () for sub in test_dict: # checking data types if isinstance (test_dict[sub], list ): res[sub] = list_delim.join([ str (ele) for ele in test_dict[sub]]) elif isinstance (test_dict[sub], tuple ): res[sub] = tuple_delim.join( list ([ str (ele) for ele in test_dict[sub]])) else : res[sub] = str (test_dict[sub]) # printing result print ( "The converted dictionary : " + str (res)) |
The original dictionary is : {'Gfg': 4, 'is': '1', 'best': [8, 10], 'geek': (10, 11, 2)} The converted dictionary : {'Gfg': '4', 'is': '1', 'best': '8-10', 'geek': '10^11^2'}
Method #2: Using dictionary comprehension
Approach
Using dictionary comprehension with type checking
Algorithm
1. Define a function named convert_dict_values_to_strings that takes three parameters test_dict, list_delim, tuple_delim.
2. Create a new dictionary using dictionary comprehension.
3. Iterate over the key-value pairs in the input dictionary.
4. If the value is a list, join the list elements with the list_delim and add the resulting string to the new dictionary.
5. If the value is a tuple, join the tuple elements with the tuple_delim and add the resulting string to the new dictionary.
6. If the value is a string, add it as it is to the new dictionary.
7. If the value is of any other type, convert it to a string and add it to the new dictionary.
8. Return the new dictionary.
Python3
def convert_dict_values_to_strings(test_dict, list_delim, tuple_delim): # Create a new dictionary using dictionary comprehension # Iterate over the key-value pairs in the input dictionary # If the value is a list, join the list elements with the `list_delim` and add the resulting string to the new dictionary # If the value is a tuple, join the tuple elements with the `tuple_delim` and add the resulting string to the new dictionary # If the value is a string, add it as it is to the new dictionary # If the value is of any other type, convert it to a string and add it to the new dictionary new_dict = {k: list_delim.join( map ( str , v)) if isinstance (v, list ) else tuple_delim.join( map ( str , v)) if isinstance (v, tuple ) else str (v) for k, v in test_dict.items()} return new_dict # Example test_dict = { 'Gfg' : 4 , 'is' : '1' , 'best' : [ 8 , 10 ], 'geek' : ( 10 , 11 , 2 )} list_delim, tuple_delim = '-' , '^' print (convert_dict_values_to_strings(test_dict, list_delim, tuple_delim)) |
{'Gfg': '4', 'is': '1', 'best': '8-10', 'geek': '10^11^2'}
Time Complexity: O(n*m), where n is the number of key-value pairs in the input dictionary and m is the size of the longest list or tuple value.
Auxiliary Space: O(n), for storing the new dictionary.
Please Login to comment...