Post

PythonPython

T
Tim Parker

Posted on 24th April 2024|18 views

0
votes

Python Sort Dictionary By Key

How can I sort a dictionary using a key in python?

Answers
P
sreelu 123

Posted on 24th April 2024

Just look at this example.

ex_dictionary = {"z": 3, "x": 1, "y": 2}

dictionary_items = ex_dictionary.items()

sorted_items = sorted(dictionary_items)

print(sorted_items)

Output:

[('x', 1), ('y', 2), ('z', 3)]

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos