dict sort in Python based on key, value

homepage-banner

#!/usr/bin/env python3

key_value ={}
key_value[2] = 56
key_value[1] = 2 
key_value[5] = 12 
key_value[4] = 24
key_value[6] = 18      
key_value[3] = 323

### sort with key
print(sorted(key_value.items(), key=lambda d:d[0]))
print(sorted(key_value.items(), key=lambda d:d[0], reverse=True))

### sort with value
print(sorted(key_value.items(), key=lambda d:d[1]))

Back to Table of Contents

Disclaimer
  1. License under CC BY-NC 4.0
  2. Copyright issue feedback me#imzye.com, replace # with @
  3. Not all the commands and scripts are tested in production environment, use at your own risk
  4. No personal information is collected.
Feedback