InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python – How to sort dictionary by values

    By admin | Last updated on Mar 20, 2016

    Sometimes we want to iterate over a dictionary in sorted order of values. We can do it by using key argument to python sorted() method and passing lambda function to it. Here is the code snippet for few scenarios:

    Sort by numeric dictionary values

    #!/usr/bin/python
    d = {"a":1, "b":5, "c":2, "be":30}
    for k in sorted(d.keys(), key=lambda x:d[x]):
      print "%s=%s" % (k, d[k])
    

    # output is from above code is:

    a=1
    c=2
    b=5
    be=30
    

    Sort by numeric dictionary values but treating them as strings

    We’ll apply str() function to values in lambda function.

    #!/usr/bin/python
    d = {"a":1, "b":5, "c":2, "be":30}
    for k in sorted(d.keys(), key=lambda x:str(d[x])):
      print "%s=%s" % (k, d[k])
    

    # output is from above code is:

    a=1
    c=2
    be=30
    b=5
    

    Suggested posts:

    1. Python type – find type of a variable
    2. Android – Show top processes using adb
    3. How to clear default app on android
    4. Rsync – using dryrun to list changed files
    5. Php regex delimiter examples
    6. Perl command line – replace multi line comments
    7. DIG quick start tutorial for DNS Lookup
    8. Benefits of using Amazon AWS – EC2
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python cookbook, Python dictionary, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress