#๐ How to sort a dictionary by values (python)? without suing inbuilt 'sort'
29 messages ยท Page 1 of 1 (latest)
@hollow wasp
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.
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.
Is this for like an assignment or coding challenge or something?
self practice
I hate this kind of challenge because when would you ever be restricted to not using the built in sort function for this?
Do you know how to do it with the builtin sorts?
builtin sort, kinda easy..
using numpy , you can do it..
yes..
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.
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..
Would you be interested in my approach?
sure..
!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)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
{'b': 1, 'c': 2, 'a': 3, 'e': 4, 'd': 5}
i find it earlier,
but, i was trying to do without dict(soretd(....))
Mm.
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.