Python Forum
list in dicitonary element problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: list in dicitonary element problem (/thread-40922.html)



list in dicitonary element problem - jacksfrustration - Oct-14-2023

ok so basically im trying to build a workout tracker app. my problem is i want to check if there is already the date inside the json fille so it won't overwrite it and if it is there then i want to append to the list in the element of the dictionary. so for example if there is already workout data for saturday the 14th of october i will append to the list of the activities (in this case there was already cycling ) and i want to append to that list to add aerobics for example. here is my relevant code


        if messagebox.askokcancel(title=f"{entry['day']} Workout Information",message=f"Activity: {entry['activity']} for {entry['unit']} minutes.\nSave this activity?"):
            if entry["day"] in saved_data:
                saved_data[entry["day"]['activity']].append(entry['activity'])
                saved_data[entry["day"]['unit']].append(entry['unit'])
            else:

                new_data={entry['day']:{"activity":[entry['activity']],"unit":[entry['unit']]}}
                saved_data.update(new_data)
        else:
            if messagebox.askokcancel(title=f"{entry['day']} Workout Information",message=f"Activity: {entry['activity']} for {entry['unit']} minutes.\nSave this activity?"):
                new_data={entry['day']:{"activity":entry['activity'],"unit":entry['unit']}}
                saved_data.update(new_data)
saved data represents the json file contents. as i have a button to generate columns of entries and labels i had to create a list of the activities that contain the day,activity and duration of the workout. the error that i get is a 'TypeError: string indices must be integers, not 'str on line 3 after the second if statement


ONE LAST NOTE: i am iterating through the list of dictionaries that contain the entry information which is why entry is present in my code. it is the assigned name for each of the dictionaries inside the list


RE: list in dicitonary element problem - menator01 - Oct-14-2023

What is the structure for the json file?
You are aware that if you are using json, that the file will have to be loaded and then rewritten to after adding the extra data?


RE: list in dicitonary element problem - jacksfrustration - Oct-14-2023

(Oct-14-2023, 12:30 PM)menator01 Wrote: What is the structure for the json file?

basically the date of the day in question is going to be the main key. inside that key are two keys. One of 'activity' and a key for 'duration' of workout. both these aforementioned keys have lists as values so i can have multiple activities registered for the same day


RE: list in dicitonary element problem - deanhystad - Oct-14-2023

Post entire error message, including the error trace. Post all relevant code, preferably a runnable example.

Without any error information I don't know if the problem is that "entry" is a list, but my guess is the error is here:
saved_data[entry["day"]['activity']].append(entry['activity'])
What is entry["day"]? Is it a date string? You are treating it as a dictionary, trying to get "activity". It should probably be this:
saved_data[entry["day"]]['activity'].append(entry['activity'])
That is not the only problem. Do you want an "activity" associated with a "unit". Your code just makes a bunch "activity" dictionaries and a bunch of "unit" dictionaries. There is no connection between them. I think you want to do this:
if messagebox.askokcancel(blah blah blah):
    saved_data.get[entry["day"], []).append({"activity": entry["activity], "unit": entry["unit"])
This uses dictionary.get(key, default) to return a new list when entry["day"] is not in saved_data.

To save in a json file, entry["activity"] and entry["unit"] must be serializable. If they are mutable (dictionaries or lists) you need to put a copy of the object in saved_data, possibly a deep copy.