Answers for "Python | Sort nested dictionary by key"

12

how to sort a dictionary by value in python

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))


# Sort by key
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
Posted by: Guest on November-27-2019
0

sort dictionary by key

dictionary_items = a_dictionary.items()
sorted_items = sorted(dictionary_items)
Posted by: Guest on June-08-2020

Code answers related to "Python | Sort nested dictionary by key"

Python Answers by Framework

Browse Popular Code Answers by Language