user1007839
user1007839

Reputation: 23

Splitting a dictionary entry in a list to two

Need to perform an operation in a dictionary which has value as a list of dictionaries

 my_dicts = 
           {"A": [
                 { 'key1 a' : 'value1',
                   'key2 a' : 'A, B, C' 
                 },

                 { 'key1 a' : 'value3',  
                   'key2 a' : 'D, E' 
                 }
                ]
              }

How can I split the first dictionary in the list whose key has two values separated by a comma ',' into two separate dictionaries in the list. i.e. The above dictionary becomes like

my_dicts = 
               {"A": [
                     { 'key1 a' : 'value1',
                       'key2 a' : 'A' 
                     },

                     { 'key1 a' : 'value1',
                       'key2 a' : 'B' 
                     },

                     { 'key1 a' : 'value1',
                       'key2 a' : 'C' 
                     },

                     { 'key1 a' : 'value3',  
                       'key2 a' : 'D' 
                     }

                      { 'key1 a' : 'value3',  
                       'key2 a' : 'E' 
                     }
                    ]
                  }

What if the no. of splits is not certain? If I could be helped with that

Upvotes: 0

Views: 1123

Answers (2)

mborst
mborst

Reputation: 68

I think this solution is more robust, since it works for arbitrary numbers of comma seperated values for both keys.

def permutateMap(X):
    result = []
    for key, value in X.items():
        splitted = value.split(',')
        if len(splitted) > 1:
            for s in splitted:
                new_dict = X.copy()
                new_dict[key] = s.strip()
                result += [new_dict]
            return splitList(result)
    return [X]

def splitList(X):
    result = []
    for entry in X:
        result += permutateMap(entry)
    return result


my_dicts = {"A": [
            { 'key1 a' : 'value1, value2', 
              'key2 a' : 'A, B, C' }, 
            { 'key1 a' : 'value3',  
              'key2 a' : 'D, E' }]}

new_dict = {}
for key, value in my_dicts.items():
    new_dict[key] = splitList(value)

print new_dict

By the way, I think it might be more appropriate/convenient to store those values not as comma seperated string but as tuples ('A', 'B', 'C'). You would not need the string operations then (split() and strip()).

Upvotes: 0

Bálint Aradi
Bálint Aradi

Reputation: 3812

You could iterate over the elements of the dictionary and create two new dictionries based on the values. Then you replace the appropriate dictionary in your list with the two new ones:

def splitdict(orig):
    dict1 = {}
    dict2 = {}
    for key, value in orig.items():
        words = value.split(",")
        if len(words) == 2:
            dict1[key] = words[0]
            dict2[key] = words[1]
        else:
            dict1[key] = value
            dict2[key] = value
    return dict1, dict2

my_dicts["A"][0:1] = splitdict(my_dicts["A"][0])

Upvotes: 1

Related Questions