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
#2
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))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple problem with functions and returns danlopek14q 10 6,619 Mar-17-2021, 05:32 PM
Last Post: danlopek14q
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 3,596 Feb-10-2021, 12:04 PM
Last Post: buran
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,369 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,586 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,860 Mar-02-2020, 10:17 AM
Last Post: buran
  A function that checks if the list is sorted pooyan89 13 15,946 Jun-18-2019, 04:02 PM
Last Post: ThomasL
  Functions to get all latitudes given a list of listing_ids Rae 4 2,760 Jun-14-2019, 02:07 PM
Last Post: Rae
  Dictionary/List Homework ImLearningPython 22 10,354 Dec-17-2018, 12:12 AM
Last Post: ImLearningPython
  defining a function to see if a list is sorted Siylo 14 6,850 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,545 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