14 ms·
Or use itemgetter: >>> from operator import itemgetter >>> dct = {'greeting': 'hello', 'thing': 'world', 'farewell': 'bye'} >>> thing, greeting = itemget
by sischoel 1y ago
Or use itemgetter:
>>> from operator import itemgetter
>>> dct = {'greeting': 'hello', 'thing': 'world', 'farewell': 'bye'}
>>> thing, greeting = itemgetter("thing", "greeting")(dct)
>>> thing
'world'
>>> greeting
'hello'
- giingyui 1y ago[dead]
- deleted 1y ago[deleted]
- notpushkin 1y agoOhhh, nice. Also, attrgetter (which also supports dotted notation to get nested attrs! Sadly, no dotted notation for itemgetter.) https://docs.python.org/3/library/operator.html#operator.attrgetter https://docs.python.org/3/library/operator.html#operator.att...
- sischoel 1y agoDotted notation would not work because the keys in a dict can also contain dots. I am not terrible familiar with them but there is something called `lenses` that comes from functional programming that should allow you to access nested structures. And I am pretty sure there must be at least one python library that implements that.