Join the discussion
Ask questions, share your code and help other Python learners. It's free.
Log in to comment, vote and follow discussions.
Continue with Google
or
Already have an account?
New to Space Python?
Check your inbox
We sent an activation link to . Open it in this browser and you'll come straight back here, logged in.
Nothing after a few minutes? Check your spam folder.
Comments 5
Join the discussion CommentAlready have an account? Log in
Use
.items():.values()gives only values,.keys()only keys (same as looping over the dict directly).items() with unpacking, nice and readable.
Dicts keep insertion order since Python 3.7, so you'll see items in the order you added them.
lambda looks scary at first but this pattern is used everywhere. Worth learning.
To loop sorted by value:
for name, d in sorted(planets.items(), key=lambda kv: kv[1]):