+ 3

Hello. Can I get help about sort an output of a dict by value

For example sort output of code below by "n3" value: dictionary1 = { "a" : {"n1": 0, "n2": 5, "n3": 9}, "b" : {"n1": 5, "n2": 4, "n3": 2}, "c" : {"n1": 2, "n2": 6, "n3": 0}, "d" : {"n1": 7, "n2": 3, "n3": 1}, }

13th Aug 2020, 11:41 AM
Houshyar
Houshyar - avatar
4 Respuestas
+ 9
Can you try this: for i in sorted(dictionary1.items(), key=lambda kv: kv[1]['n3']): print(*i) https://code.sololearn.com/c492FReZA2EF/?ref=app
13th Aug 2020, 1:41 PM
Lothar
Lothar - avatar
+ 5
ok - can you show us a sample how the output should look like? Thanks!
13th Aug 2020, 1:24 PM
Lothar
Lothar - avatar
+ 1
Output should be like string below: c = n1: 2, n2 : 6, n3 : 0 d = n1: 7, n2 : 3, n3 : 1 b = n1: 5, n2 : 4, n3 : 2 a = n1: 0, n2 : 5, n3 : 9 Thanks 👍
13th Aug 2020, 1:39 PM
Houshyar
Houshyar - avatar
+ 1
Thank you Lothar. Amazing
13th Aug 2020, 2:01 PM
Houshyar
Houshyar - avatar