dict sort in Python based on key, value
#!/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
- License under
CC BY-NC 4.0
- Copyright issue feedback
me#imzye.com
, replace # with @ - Not all the commands and scripts are tested in production environment, use at your own risk
- No personal information is collected.
Feedback