Cascade get value from dictionary in Python

Several times I’ve been faced with the problem:

Get a value from a dictionary. If the key is not present, then get from another key.

Normally, the implementation would go like this:

Or, even worse, with try-catch statements. That’s fine if the sequence is fixed, but what if you want to have some flexibility? Well, my solution is like this:

Pretty, isn’t it?

Note: This is a post transferred from Laurii for historical and consolidation purposes.


A little experiment: If you find this post and ad below useful, please check the ad out :-)




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top