Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Sort a Dictionary by Key or by Value

Python Sort a Dictionary by Key or by Value

Document Object Reference Manual Python3 Examples

Given a dictionary, sort it by key or by value.

Example 1: sort by key

def

dictionairy

(

)

:

Declare the dictionary

key_value

={}

Initialization

key_value

[

2

]

=

56

key_value

[

1

]

=

2

key_value

[

5

]

=

12

key_value

[

4

]

=

24

key_value

[

6

]

=

18

key_value

[

3

]

=

323

print

(

"

Sort by key:

"

)

sorted(key_value) returns a reordered list

Dictionary sorted by key

for

i

in

sorted

(

key_value

)

:

print

(

(

i

,

key_value

[

i

]

)

,

end

=

"

"

)

def

main

(

)

:

Calling a function

dictionairy

(

)

Main function

if

name

==

"

main

"

:

main

(

)

The output of the code above is:



Sort by key:

(1, 2) (2, 56) (3, 323) (4, 24) (5, 12) (6, 18) 

Example 2: sort by value

def

dictionairy

(

)

:

Declare the dictionary

key_value

={}

Initialization

key_value

[

2

]

=

56

key_value

[

1

]

=

2

key_value

[

5

]

=

12

key_value

[

4

]

=

24

key_value

[

6

]

=

18

key_value

[

3

]

=

323

print

(

"

Sort by value:

"

)

print

(

sorted

(

key_value

.

items

(

)

,

key

=

lambda

kv

:

(

kv

[

1

]

,

kv

[

0

]

)

)

)

def

main

(

)

:

dictionairy

(

)

if

name

==

"

main

"

:

main

(

)

The output of the code above is:



sort by value:

[(1, 2), (5, 12), (6, 18), (4, 24), (2, 56), (3, 323)]

Example 3: sort a list of dictionaries

lis

=

[

{

"

name

"

:

"

Taobao

"

,

"

age

"

:

100

}, {

"

name

"

:

"

Runoob

"

,

"

age

"

:

7

}, {

"

name

"

:

"

Google

"

,

"

age

"

:

100

}, {

"

name

"

:

"

Wiki

"

,

"

age

"

:

200

}

]

Sort by age in ascending order

print

(

"

List sorted by age in ascending order:

"

)

print

(

sorted

(

lis

,

key

=

lambda

i

:

i

[

'

age

'

]

)

)

print

(

"

\r

"

)

First sort by age, then by name

print

(

"

List sorted by age and name:

"

)

print

(

sorted

(

lis

,

key

=

lambda

i

:

(

i

[

'

age

'

]

,

i

[

'

name

'

]

)

)

)

print

(

"

\r

"

)

Sort by age in descending order

print

(

"

List sorted by age in descending order:

"

)

print

(

sorted

(

lis

,

key

=

lambda

i

:

i

[

'

age

'

]

,

reverse

=

True

)

)

The output of the code above is:



# Sort the list by age in ascending order:

[{'name': 'Runoob', 'age': 7}, {'name': 'Taobao', 'age': 100}, {'name': 'Google', 'age': 100}, {'name': 'Wiki', 'age': 200}]



Sort the list by age and name:

[{'name': 'Runoob', 'age': 7}, {'name': 'Google', 'age': 100}, {'name': 'Taobao', 'age': 100}, {'name': 'Wiki', 'age': 200}]



# Sort the list by age in descending order:

[{'name': 'Wiki', 'age': 200}, {'name': 'Taobao', 'age': 100}, {'name': 'Google', 'age': 100}, {'name': 'Runoob', 'age': 7}]

Document Object Reference Manual Python3 Examples