Home > Software design >  How to create one dictionary from for loop
How to create one dictionary from for loop

Time:09-22

I am very new to coding and I am trying to iterate through a list and create a single dictionary with the same keys. My code at the moment is creating about a 1000 dictionaries in the correct format with the same keys but I can't figure out how to make these into one dictionary.

for salaries, match in matching_salaries_non_employees.items():

   final_match.update(matches)

   Power_match = (match['Status']['Power'])  ": "   (salaries)
    
   Length_match = (match['Status']['Length'])  ": "   (match['Break']['Temp'])
        
   duration_Status = (match ['Status'][str('duration')])
   duration_Break = (match ['Break']['duration'])
   duration_match = (str(duration_Status)   ': '   str(duration_Break))
    
   freq_Status = (match ['Status'][str('freq')])
   freq_Break = (match ['Break']['freq'])
   freq_match = (str(freq_Status)   ": "   str(freq_Break))
    
   start_times = (match['Status']['start time'])   ": "   (match['Break']['start time'])

   matches = {'Power': [power_match], 'Length': [Length_match], 'Duration': [duration_match], 'Freq': [freq_match], 'Start Times': [start_times]}

An example of a part of my output looks like;

{'Power': ['high: high'], 'Length': ['Long: Short'], 'Duration': ['2.22: 1.01'], 'Freq': ['0.081229: 0.079503'], 'Start Times': ['10.10.08: 11.09.16']}
{'Power': ['med-high: med-high'], 'Length': ['Long: Long'], 'Duration': ['1.16: 0.81'], 'Freq': ['0.0988: 0.565'], 'Start Times': ['17.08.98: 31:01:03']}

Whereas I would like it to be combined into one dictionary this so that I can save it to a single csv file

CodePudding user response:

Easiest and most straightforward solution would be to use a defaultdict

from collections import defaultdict

matches = defaultdict(list)

for salaries, match in matching_salaries_non_employees.items():

   Power_match = (match['Status']['Power'])  ": "   (salaries)
    
   Length_match = (match['Status']['Length'])  ": "   (match['Break']['Temp'])
        
   duration_Status = (match ['Status'][str('duration')])
   duration_Break = (match ['Break']['duration'])
   duration_match = (str(duration_Status)   ': '   str(duration_Break))
    
   freq_Status = (match ['Status'][str('freq')])
   freq_Break = (match ['Break']['freq'])
   freq_match = (str(freq_Status)   ": "   str(freq_Break))
    
   start_times = (match['Status']['start time'])   ": "   (match['Break']['start time'])

   matches['Power'].append(Power_match)
   matches['Length'].append(Length_match)
   matches['Duration'].append(duration_match)
   matches['Freq'].append(freq_match)
   matches['Start Times'].append(start_times)

CodePudding user response:

The answer by @Matias Cicero is great, but the explicit initialization is also very simple:

keys = ['Power', 'Length', 'Duration', 'Freq', 'Start Times']
matches = {k: [] for k in keys}

which would replace the lines

from collections import defaultdict
matches = defaultdict(list)
  • Related