#๐Ÿ”’ How to sort a dictionary by values (python)? without suing inbuilt 'sort'

29 messages ยท Page 1 of 1 (latest)

hollow wasp
#

my_dict={'a':3, 'b':1, 'c':2, 'd':5, 'e':4}
the final sould be like ...
my_dict={'b':1, 'c':2, 'a':3, 'e':4, 'd':5}

eternal auroraBOT
#

@hollow wasp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

thin musk
#

Create a list of the items of the dictionary, then sort by the second element of each item tuple.

#

Which would be the value.

#

Then you can give that list to the dict constructor.

hearty spear
#

Is this for like an assignment or coding challenge or something?

hearty spear
#

I hate this kind of challenge because when would you ever be restricted to not using the built in sort function for this?

thin musk
#

Do you know how to do it with the builtin sorts?

hollow wasp
thin musk
#

Numpy may be overkill.

#

One-lineable easily enough.

hollow wasp
thin musk
#

Well, pick a sorting algorithm.

#

There are several.

#

If you're set on writing your own.

#

That's the first step.

#

In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require...

#

Python's native sorting is based on Timsort.

hollow wasp
# thin musk Create a list of the `items` of the dictionary, then sort by the second element ...

Thanks, with this tip, did both sort, by key and by value..

def sort_byKey(dict1: dict):
dict1_list = list(dict1.items())

for i in range(len(dict1_list)):
    for j in range(len(dict1_list)-1):
        if dict1_list[j][0] > dict1_list[j+1][0]:
            dict1_list[j], dict1_list[j+1] = dict1_list[j+1], dict1_list[j]
KeySorted_dict = {}
for k, v in dict1_list:
    KeySorted_dict[k] = v
return KeySorted_dict

print(sort_byKey({"apple": 3, "orange": 1,
"banana": 2, "grape": 5, "kiwi": 4}))

output : {'apple': 3, 'banana': 2, 'grape': 5, 'kiwi': 4, 'orange': 1}

SORT BY VALUE

def sort_byValue(dict1: dict):
dict1_list = list(dict1.items())

for i in range(len(dict1_list)):
    for j in range(len(dict1_list)-1):
        if dict1_list[j][1] > dict1_list[j+1][1]:
            dict1_list[j], dict1_list[j+1] = dict1_list[j+1], dict1_list[j]
KeySorted_dict = {}
for k, v in dict1_list:
    KeySorted_dict[k] = v
return KeySorted_dict

print(sort_byValue({"apple": 3, "orange": 1,
"banana": 2, "grape": 5, "kiwi": 4}))

output : {'orange': 1, 'banana': 2, 'apple': 3, 'kiwi': 4, 'grape': 5}

#

took a help from gpt, for understanding [0][1] part, else by self..

thin musk
#

Would you be interested in my approach?

hollow wasp
thin musk
#

!e py my_dict = {'a': 3, 'b': 1, 'c': 2, 'd': 5, 'e': 4} result = dict(sorted(my_dict.items(), key=lambda kv: kv[1])) print(result)

eternal auroraBOT
hollow wasp
thin musk
#

Mm.

eternal auroraBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.