'Converting dict comprehension into normal form
I need to convert this in normal for loop form how can i do this
read = {
year: {
month: read_data_of_a_month(path) for month in months
}
for year in range(2000,2013)
}
Solution 1:[1]
Just use a nested for loop:
read = {}
for year in range(2000, 2013):
year_dict = {}
for month in months:
year_dict[month] = read_data_of_a_month(path)
read[year] = year_dict
Solution 2:[2]
read = {}
for year in range(2000, 2013):
m = {}
for month in months:
m[month] = read_data_of_a_month(path)
read[year] = m
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Netwave |
Solution 2 | Dharman |