I am searching for pythonic way of getting first N element from a DefaultDict. I searched for it in few forums. Couldn't find any useful solution. I tried the following but this doesn't stop the execution as expected.
Code:
d_2 = defaultdict(list)
i = -1
for x,y in sorted(d_2.items(), reverse = True):
i = i + 1
if i <= 20:
print ("%d, %s" %(i,x))
most_used_val_bar.add(str(x),y)
else:
break
most_used_val_bar.render_to_file(directory + "/most_used_values.svg")
But this code doesn't break when i goes over 20, I am using this for loop to plot using Pygal Plotting Library