0

So, I need to print the sales of my hotel management system both in ascending and descending order. I found these codes down below which solved my problem (yes, I'm a total beginner).

    def ascend(self, dict):
        print({k: v for k, v in sorted(dict.items(), key=lambda item: item[1])})

    def descend(self, dict):
        print({k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse=True)})

It does print out the sales but the output has a single quote around the key, curly braces, and only in one line which is the total opposite of my desired output. How do I sort in ascending and descending while printing each in a new line without the curly braces and the single quote?

Big thanks to anyone who could help!

sriracha
  • 3
  • 2
  • Does this answer your question? [How to print a dictionary line by line in Python?](https://stackoverflow.com/questions/15785719/how-to-print-a-dictionary-line-by-line-in-python) – awesoon Jun 17 '21 at 05:14

1 Answers1

0

Here's one way to do it

def ascend(dict):
   sorted_dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1])}
   for key, value in sorted_dict.items():
      print(key, ',', value)

def descend(self, dict):
   sorted_dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1], reverse=True)}
   for key, value in sorted_dict.items():
      print(key, ',', value)

What I did here is I saved the sorted dictionary in a variable and then iterated over that dictionary to print its content in the desired format.

Hussain Pettiwala
  • 1,406
  • 8
  • 16