Home > Back-end >  How to update elements in a dictionary in Python?
How to update elements in a dictionary in Python?

Time:10-07

I have a list of dictionary in which there are other lists of object

{'Kitchen_Activity': [date= 2009-10-16, start time= 08:45:38, end time= 08:58:52, duration= 0 days 00:13:14 ]}
{'Chores': [date= 2009-10-16, start time= 08:59:02, end time= 09:14:47, duration= 0 days 00:15:45 ]}
{'Kitchen_Activity': [date= 2009-10-16, start time= 09:14:40, end time= 09:14:54, duration= 0 days 00:00:14 ]}
{'Chores': [date= 2009-10-16, start time= 09:30:40, end time= 09:58:54, duration= 0 days 00:28:14 ]}
{'Kitchen_Activity': [date= 2009-10-16, start time= 10:14:40, end time= 10:14:54, duration= 0 days 00:00:14 ]}
{'Shower': [date= 2009-10-16, start time= 11:14:40, end time= 11:40:40, duration= 0 days 00:26:00 ]}

and I want to achive this kind of list:

{'Kitchen_Activity': [date= 2009-10-16, start time= 08:45:38, end time= 08:58:52, duration= 0 days 00:13:14 ], [date= 2009-10-16, start time= 09:14:40, end time= 09:14:54, duration= 0 days 00:00:14 ], [date= 2009-10-16, start time= 10:14:40, end time= 10:14:54, duration= 0 days 00:00:14 ]}
{'Chores': [date= 2009-10-16, start time= 08:59:02, end time= 09:14:47, duration= 0 days 00:15:45 ], [date= 2009-10-16, start time= 09:30:40, end time= 09:58:54, duration= 0 days 00:28:14 ]}
{'Shower': [date= 2009-10-16, start time= 11:14:40, end time= 11:40:40, duration= 0 days 00:26:00 ]}

What I've tried doing is this

def generateActivity(self, date, name, ts, te, dur):
        activities = {}
        activity = Activity().generateInstance(date, name, ts, te, dur)

        if activity.name not in activities.keys():
            activities[activity.name] = []
            activities[activity.name].append(activity)
        else:
            for key, item in activities.items():
                if key == activity.name:
                    item.append(activity)
        return activities

The list I put up there is the output of this function that I call in the main class. What's wrong with what I'm doing?

CodePudding user response:

The list which you want to recieve follows the order, which isn't allowed for key values in a dictionary

{
    a:[],[]
    b:[],[]
}

Instead you should try for

{
    a:[[],[]]
    b:[[],[]]
}

CodePudding user response:

I take your list of dictionary as dicts

If you want the above result, you make a dictionary value string (used json.dumps())

import json
update_dict = {}
for dict in dicts:
    current_key = list(dict.keys())[0]
    if not current_key in update_dict:
        update_dict[current_key] = json.dumps(dict[current_key])
    else:
        update_dict[current_key] = update_dict[current_key]  ", " json.dumps(dict[current_key])

Otherwise you get the value list of list value.

update_dict = {}
for dict in dicts:
    current_key = list(dict.keys())[0]
    if not current_key in update_dict:
       update_dict.update({current_key:[]})
       update_dict[current_key].append(dict[current_key])
    else:
        update_dict[current_key].append(dict[current_key])
  • Related