Skip to content Skip to sidebar Skip to footer

Python For Loop Only Returning Last Value Of A Dictionary

I'm trying to create a json dump with xyz coordinates in python, however the for loop im using to go trough different groups only returns the last group self.group_strings = ['CHIN

Solution 1:

In the for loop, you're overwriting data at every iteration with data = coords_data. If data is a list, then use data.append(coords_data) instead to add new data to it at each iteration. Note that you'll need to initialize it before the for loop with data = []

Essentially:

data = []
for grp_str in group_strings:
   data.append(self.point_dict[grp_str]['Coords'])

Solution 2:

Your with block is outside the for loop so it's executed after loop finishes and has only access to the last element because that's the state with which the loop terminates.

But if you open a with inside your loop block everytime, you'll again get the same result, so you've to open it with append mode 'a+'

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   for grp_str in self.group_strings:
       coords_data = self.point_dict[grp_str]['Coords']
       data = coords_data
       # with is now inside the for loopwithopen("data_file.json", "a+") as write_file:
           json.dump(data, write_file)

A even better way would be to run the loop inside the context manager.

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   withopen("data_file.json", "w") as write_file:
       for grp_str in self.group_strings:
           coords_data = self.point_dict[grp_str]['Coords']
           data = coords_data
           json.dump(data, write_file)

Solution 3:

You overwrite your data variable after each iteration in your for loop, hence you only get that last iteration. You'll need to initialize something to dump each iteration of the data into a "results" data of some sort:

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

data = []
if reply == QMessageBox.Yes:
   for grp_str in self.group_strings:
       data.append(self.point_dict[grp_str]['Coords'])

   withopen("data_file.json", "w") as write_file:
       json.dump(data, write_file)

Solution 4:

You need to add your coords_data to a list, where they will be remembered, and then write that list onto the file, right now data = coords_data only remembers the last value of coords_data and it writes that onto the file.

self.group_strings = ['CHIN', 'L_EYE_BROW', 'R_EYE_BROW', 'L_EYE', 'R_EYE', 'T_NOSE', 'B_NOSE', 'O_LIPS', 'I_LIPS']

if reply == QMessageBox.Yes:
   datas = []
   for grp_str in self.group_strings:
       #Append all values to a list
       datas.append(self.point_dict[grp_str]['Coords'])

   #Write that list to filewithopen("data_file.json", "w") as write_file:
       json.dump(datas, write_file)

Post a Comment for "Python For Loop Only Returning Last Value Of A Dictionary"