Cascade get value from dictionary in Python

Posted by in Software, Tutorial

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 :-)