Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting List
#5
How long did you think about this before giving up? You got a list of sorted numbers because you are sorting the values(), not the items(). You would have noticed this if you tried printing dt.values(). Spend more time trying to answer these kind of questions yourself. Your programming skills will increase much faster if you fix your own problems. Debugging is a great teacher. It produces a deeper and wider understanding than somebody giving you the answer.
dt = {'Finley' : 10, 'Evie' : 0, 'P1' : 0, 'P2' : 5, 'P1' : 0, 'P2' : 5, 'Finley' : 15, 'Evie' : 5}

print(dt.values())
Output:
dict_values([15, 5, 0, 5])
Notice that the names (keys) are gone. The are not going to reappear when sorted.

You cannot sort a dictionary, but you can put the items in a list and sort them.
dt = {'Finley' : 10, 'Evie' : 0, 'P1' : 0, 'P2' : 5, 'P1' : 0, 'P2' : 5, 'Finley' : 15, 'Evie' : 5}

sorted_dt = sorted(dt.items(), key=lambda x: (int(x[1]), x[0]))
print(sorted_dt)
Output:
[('P1', 0), ('Evie', 5), ('P2', 5), ('Finley', 15)]
And of course now that dt is a dictionary you cannot have duplicate names (keys). Duplicate keys are replaced, so the dt dictionary is:
dt = {'Finley': 15, 'Evie': 5, 'P1': 0, 'P2': 5}
Once you have a sorted list of items you can use them to construct a dictionary that is sorted.
dt = {'Finley' : 10, 'Evie' : 0, 'P1' : 0, 'P2' : 5, 'P1' : 0, 'P2' : 5, 'Finley' : 15, 'Evie' : 5}
sorted_dt_items = sorted(dt.items(), key=lambda x: (int(x[1]), x[0]))
sorted_dt = {key:value for key, value in sorted_dt_items}
print(sorted_dt)
Output:
{'P1': 0, 'Evie': 5, 'P2': 5, 'Finley': 15}
Frankduc likes this post
Reply


Messages In This Thread
Sorting List - by finndude - Jan-25-2022, 05:01 PM
RE: Sorting List - by ndc85430 - Jan-25-2022, 05:57 PM
RE: Sorting List - by deanhystad - Jan-25-2022, 06:23 PM
RE: Sorting List - by Frankduc - Jan-25-2022, 06:49 PM
RE: Sorting List - by deanhystad - Jan-25-2022, 07:06 PM
RE: Sorting List - by Frankduc - Jan-25-2022, 07:13 PM
RE: Sorting List - by ndc85430 - Jan-26-2022, 06:53 AM
RE: Sorting List - by Pedroski55 - Jan-26-2022, 11:40 PM
RE: Sorting List - by DeaD_EyE - Jan-27-2022, 12:29 PM
RE: Sorting List - by Pedroski55 - Jan-27-2022, 09:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Sorting Problem ZZTurn 5 1,513 Sep-22-2022, 11:23 PM
Last Post: ZZTurn
  sorting a list of lists by an element leapcfm 3 2,053 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,872 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  list sorting question DPaul 5 3,013 Jun-17-2020, 02:23 PM
Last Post: ndc85430
  sorting list of lists pframe 5 34,531 Apr-17-2020, 09:31 PM
Last Post: Larz60+
  sorting list arian29 2 2,240 Feb-02-2020, 10:31 AM
Last Post: ndc85430
  Converting parts of a list to int for sorting menator01 2 2,361 Nov-03-2019, 03:00 PM
Last Post: menator01
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,333 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  sorting a list of tuples based on date bluefrog 2 5,943 Aug-10-2018, 02:31 AM
Last Post: ichabod801
  Sorting list of lists with string and int Otbredbaron 6 4,476 May-07-2018, 06:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

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