Skip to content

Instantly share code, notes, and snippets.

@LefterisJP
Last active January 27, 2024 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LefterisJP/2cf6fb17192d605c5df0f5ece2cc515e to your computer and use it in GitHub Desktop.
Save LefterisJP/2cf6fb17192d605c5df0f5ece2cc515e to your computer and use it in GitHub Desktop.
Make else case work without def or lambda
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