I need a little help to solve this problem. I have a list like that
list_data = [(2014, 0.2, 4), (2014, 0.3, 2), (2015, 0.2, 1), (2015, 0.3, 33), (2015, 0.4, 56)]
and I want to create a dictionary like that
{2014: {0.2: 4, 0.3: 2}, {2015: {0.2: 1, 0.3: 33, 0.4: 56}}
an easy way I have is :
my_dict = {}
for (year, ntop, value) in list_data:
if year in my_dict:
my_dict[year][ntop] = value
else:
my_dict[year] = {ntop: value}
but I want to have that in list comprehension. I have build this solution :
my_dict = {year: {ntop: value for (year_check, ntop, value) in list_data if year_check == year} for (year,_ ,_ ) in list_data}
but as you can see, I increase the complexity as I loop 2 times instead of 1.
Is there a 1 loop solution of this problem or not?
Thanks