Python Forum

Full Version: Functions returns content of dictionary as sorted list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to create a function in python 3.6 that returns a dictionary with count being the key and list being the value to gather all items with the same count.

This is what I have for code so far:
def sorted_dict_content(key_val):
    return [{k: key_val[k]}
    for k in sorted(key_val.keys(), reverse=True)]
While this code is functional, I am wondering if there is a way I can get the same output by creating a blank list, appending the key-value pairs to it, and returning this list.

Thanks for helping!
Any list comprehension of the form data = [expression(x) for x in sequence] can be written as a for loop:

data = []
for x in sequence:
   data.append(expression(x))