Reputation: 75
I need to sort this dictionary by the second value in the list. That is 341
for the first dictionary "key-value" pair.
{'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver',
335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman':
["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah
Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie
Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury
(uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice)
(uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2],
'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw',
97], 'Robert Pralgo': ['Thanos Reader', 344]}
From a few Stack Overflow questions I found OrderedDict
. Could it be used for this purpose? But I was unable to achieve this.
PS: I want to get this sorted dictionary as dictionary, not tuple.
Upvotes: 2
Views: 52
Reputation: 71610
Or sort by def
:
d = {'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver', 335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Robert Pralgo': ['Thanos Reader', 344]}
def f(x):
return x[1][1]
print(dict(sorted(d.items(), key=f)))
Upvotes: 0
Reputation: 106901
Dicts are ordered in Python 3.6+, so you can simply sort the dict items with a custom key:
d = {'Ariana Greenblatt': ['Young Gamora', 341], 'Stan Lee': ['Schoolbus Driver', 335], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Letitia Wright': ['Shuri', 90], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357], 'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Robert Pralgo': ['Thanos Reader', 344]}
print(dict(sorted(d.items(), key=lambda x: x[1][1])))
This outputs:
{'Chris Evans': ['Steve Rogers / Captain America', 2], 'Chris Hemsworth': ['Thor Odinson', 6], 'Chadwick Boseman': ["T'Challa / Black Panther", 46], 'Anthony Mackie': ['Sam Wilson / Falcon', 70], 'Letitia Wright': ['Shuri', 90], 'Tom Vaughan-Lawlor': ['Ebony Maw', 97], 'Florence Kasumba': ['Ayo', 114], 'Carrie Coon': ['Proxima Midnight (voice)', 329], 'Samuel L. Jackson': ['Nick Fury (uncredited)', 332], 'Stan Lee': ['Schoolbus Driver', 335], 'Ariana Greenblatt': ['Young Gamora', 341], 'Ameenah Kaplan': ["Gamora's Mother", 342], 'Robert Pralgo': ['Thanos Reader', 344], 'Kenneth Branagh': ['Asgardian Distress Call (voice) (uncredited)', 357]}
Or prior to Python 3.6, you can use collections.OrderedDict
instead:
from collections import OrderedDict
d = OrderedDict([('Ariana Greenblatt', ['Young Gamora', 341]), ('Stan Lee', ['Schoolbus Driver', 335]), ('Anthony Mackie', ['Sam Wilson / Falcon', 70]), ('Chadwick Boseman', ["T'Challa / Black Panther", 46]), ('Letitia Wright', ['Shuri', 90]), ('Ameenah Kaplan', ["Gamora's Mother", 342]), ('Florence Kasumba', ['Ayo', 114]), ('Carrie Coon', ['Proxima Midnight (voice)', 329]), ('Samuel L. Jackson', ['Nick Fury (uncredited)', 332]), ('Kenneth Branagh', ['Asgardian Distress Call (voice) (uncredited)', 357]), ('Chris Evans', ['Steve Rogers / Captain America', 2]), ('Chris Hemsworth', ['Thor Odinson', 6]), ('Tom Vaughan-Lawlor', ['Ebony Maw', 97]), ('Robert Pralgo', ['Thanos Reader', 344])])
print(OrderedDict(sorted(d.items(), key=lambda x: x[1][1])))
Upvotes: 5