I would like to share another solution to make display_table function for Guided Project: Profitable App Profiles for the App Store and Google Play Markets 10/14. I used comprehensions, lamda and sort() function, probably these topics will be covered in the next courses.
Original solution:
> def display_table(dataset, index):
> table = freq_table(dataset, index)
> table_display = []
> for key in table:
> key_val_as_tuple = (table[key], key)
> table_display.append(key_val_as_tuple)
>
> table_sorted = sorted(table_display, reverse = True)
> for entry in table_sorted:
> print(entry[1], ':', entry[0])
My solution:
> def display_table(dataset, index):
> table = freq_table(dataset, index)
> table_display = [(k,v) for k,v in table.items()]
> table_display.sort(key = lambda x: x[1], reverse = True) # key argument for sorting (this is not about key in dictionary) is the second item in tuple, namely value not dictionary key
> for item in table_display:
> print(item[0],":",item[1])