Sep-17-2019, 01:40 PM
I finally figured this out, but I'm not totally understanding why it works.
I created my dictionary (d) with keys and values. Now I need to print out the dictionary sorted from most to least occurrences:
sortedbyval = ()
Why is that not sufficient to define an empty list?
Then, I had line 2 as
for key, val in sortedbyval(d.items()):
With "list" being a data structure type (I think), why should the syntax include "list" instead of the actual list name? Would the latter not make sense because at line 2, there are no key, val in sortedbyval since sortedbyval is still empty?
If you can shed any light on this then I'd appreciate it!
I created my dictionary (d) with keys and values. Now I need to print out the dictionary sorted from most to least occurrences:
sortedbyval = list() #creating empty list that will include sorted values for key, val in list(d.items()): sortedbyval.append((val, key)) sortedbyval.sort(reverse=True) for key, val in sortedbyval: print(key, val)I understand that key and val are arbitrary counter variables. I think what confuses me most is the word "list." I initially had line 1 as
sortedbyval = ()
Why is that not sufficient to define an empty list?
Then, I had line 2 as
for key, val in sortedbyval(d.items()):
With "list" being a data structure type (I think), why should the syntax include "list" instead of the actual list name? Would the latter not make sense because at line 2, there are no key, val in sortedbyval since sortedbyval is still empty?
If you can shed any light on this then I'd appreciate it!