Python Forum
Functions returns content of dictionary as sorted list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Functions returns content of dictionary as sorted list
#1
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!
Reply


Messages In This Thread
Functions returns content of dictionary as sorted list - by kyletremblay15 - Nov-21-2019, 08:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 16,579 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
  Simple problem with functions and returns danlopek14q 10 6,782 Mar-17-2021, 05:32 PM
Last Post: danlopek14q
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,712 Feb-10-2021, 12:04 PM
Last Post: buran
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,444 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  Loop through elements of list and include as value in the dictionary Rupini 3 2,656 Jun-13-2020, 05:43 AM
Last Post: buran
  How can details be dynamically entered into a list and displayed using a dictionary? Pranav 5 2,931 Mar-02-2020, 10:17 AM
Last Post: buran
  Functions to get all latitudes given a list of listing_ids Rae 4 2,812 Jun-14-2019, 02:07 PM
Last Post: Rae
  Dictionary/List Homework ImLearningPython 22 10,575 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython
  defining a function to see if a list is sorted Siylo 14 6,988 Nov-29-2018, 05:25 PM
Last Post: wavic
  making a dictionary from a list, one key with multiple values in a list within a list rhai 4 3,611 Oct-24-2018, 06:40 PM
Last Post: LeSchakal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020