How to Sort a Dictionary in Python by the Values

The Python programming language has several different ways of storing data. The dictionary is one way Python can store data. A dictionary stores data as key-value pairs, just like a real dictionary stores data as word-definition pairs. Dictionaries can be sorted by either the key or the value. The syntax for sorting a dictionary is straightforward, and even total beginners to Python can pick it up quickly.

Things You'll Need

  • Computer with Python 3.2 programming language installed (see Resource)
Show More

Instructions

    • 1

      Open the IDLE text editor that comes with the Python download. Find the IDLE text editor in Program Files (or Applications for Macintosh), in the Python directory. A blank source code file opens in the IDLE text editor window.

    • 2

      Import "itemgetter" from the module "operator." Write the following at the top of the source code file to import this method:

      from operator import itemgetter

    • 3

      Declare a dictionary and fill it with values. You can write something like this:

      shoppingList = {'milk':2.19, 'bread':3.09, 'coffee':4.59, 'eggs':1.79, 'cheese':4.99}

    • 4

      Print out the dictionary sorted by value. To sort by value, you need to use the sorted() function and "itemgetter," like this:

      print(sorted(shoppingList.items(), key=itemgetter(1)))

    • 5

      Press F5 to run the program. The program outputs the dictionary sorted by value, and that output looks like this:

      [('eggs', 1.79), ('milk', 2.19), ('bread', 3.09), ('coffee', 4.59), ('cheese', 4.99)]

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Create a Dictionary in Python

    In the Python programming language, a dictionary is a data structure that maps unique keys to values. In other programming languages, however,...

  • How to Create an Array in Python

    Arrays are useful and fundamental structures that exist in every high-level language. In Python, arrays are native objects called "lists," and they...

  • Python Diet

    Pythons are hugely popular pets among snake lovers, but they need special care and attention beyond what other common pets require. A...

  • How to Program an Electronic Dictionary

    Electronic dictionaries are ubiquitous in 21st-century living. They're embedded in document makers and electronic readers, are an integral part of software for...

  • How to Learn Python Computer Language

    Python is an accessible programming language that you can use to create all kinds of software and web applications. Many books and...

  • How to Convert a CSV File to a Graph in Python

    Most database and spreadsheet applications can conveniently output table data in the form of CSV (comma-separated-values) files. While CSV files are handy...

  • How to Get the System Date From Python

    Python is a dynamic, interpreted programming language. Its features include a readable syntax, exception-based error handling, and an extensive library of modules...

  • How to Access Data in a Dictionary

    Online dictionaries are free resources that provide a faster way to look up words and their meanings. If you misspell a word,...

  • How to Make Games Using Python for Beginners

    Python and its free game development library, Pygame, handle a lot of the detail work of game development for you, allowing you...

Related Ads

Featured