Home > Net >  Adding to a value list of a list of dictionary
Adding to a value list of a list of dictionary

Time:06-03

I have spent too long on this and tried various things, but kept getting no where or error with unhashable type: 'list', any help or direction would be greatly appreciate it.

I have a list of dictionaries in python:

my_dict = [{'name': 'john doe', 'favorite_color': ['red']}, {'name': 'jane doe', 'favorite_color': ['pink']}, {'name': 'john doe', 'favorite_color': ['blue', 'green']}, {'name': 'jane doe', 'favorite_color': ['grey', 'white']}]

I'm trying to consolidate a list so that the output looks like:

my_dict = [{'name': 'john doe', 'favorite_color': ['red','blue','green']}, {'name': 'jane done'}, 'favorite_color': ['pink', 'grey', 'white']}]

CodePudding user response:

Just for example...

my_dict = [
    {'name': 'john doe', 'favorite_color': ['red']},
    {'name': 'jane doe', 'favorite_color': ['pink']},
    {'name': 'john doe', 'favorite_color': ['blue', 'green']},
    {'name': 'jane doe', 'favorite_color': ['grey', 'white']}
]

new_list = []
for item in my_dict:
    current_names = {val['name']: i for i, val in enumerate(new_list)}
    name_index = current_names.get(item['name'])
    if name_index is not None:
        new_list[name_index]['favorite_color'].extend(item['favorite_color'])
    else:
        new_item = {'name': item['name'], 'favorite_color': item['favorite_color']}
        new_list.append(new_item)

Output:

new_list
[{'name': 'john doe', 'favorite_color': ['red', 'blue', 'green']}, {'name': 'jane doe', 'favorite_color': ['pink', 'grey', 'white']}]

CodePudding user response:

You can iterate through all of the dictionaries in the list and build a new dictionary:

my_dict = [{'name': 'john doe', 'favorite_color': ['red']}, {'name': 'jane doe', 'favorite_color': ['pink']}, {'name': 'john doe', 'favorite_color': ['blue', 'green']}, {'name': 'jane doe', 'favorite_color': ['grey', 'white']}]


favorite_colors = {}

for d in my_dict:
    
    if d['name'] not in favorite_colors:
        # start a new key-value pair with an empty list for the name
        #    if one isn't already in the new dict
        favorite_colors[d['name']] = []

    # add favorite_color for each name, using d['name'] as the key for the new dict
    favorite_colors[d['name']]  = d['favorite_color']

print(favorite_colors)
{'john doe': ['red', 'blue', 'green'], 'jane doe': ['pink', 'grey', 'white']}

This may be easier to work with than keeping a list of dictionaries.

If you need to preserve the input as a list of dictionaries, you can iterate over it again and create a new list:

favorite_colors = {}
for d in my_dict:
    if d['name'] not in favorite_colors:
        favorite_colors[d['name']] = []
    favorite_colors[d['name']]  = d['favorite_color']

list_of_dicts = []
for name, list_of_colors in favorite_colors.items():
    list_of_dicts.append({'name': name, 'favorite_color': list_of_colors})

print(list_of_dicts)
[{'name': 'john doe', 'favorite_color': ['red', 'blue', 'green']}, {'name': 'jane doe', 'favorite_color': ['pink', 'grey', 'white']}]

Iterating via two different for loops may not be the most efficient coding, but in this case I hope it may help to see what's going on.

  • Related