Last active
January 27, 2024 16:03
-
-
Save LefterisJP/2cf6fb17192d605c5df0f5ece2cc515e to your computer and use it in GitHub Desktop.
Make else case work without def or lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
if input('Enter 1 for simple case:') == '1': | |
data = {'a': 1, 'b': 2} | |
value_getter = operator.getitem | |
else: | |
data = {'a': {'innerkey': 1}, 'b': {'innerkey': 2}} | |
value_getter = lambda x, y: x[y]['innerkey'] | |
print(value_getter(data, 'a')) | |
print(value_getter(data, 'b')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment