4 ms·
As long as we're playing code golf, try this for python: def values_in(dict, keys): marker = object() return [x for x in (dict.get(
by anon4 12y ago
As long as we're playing code golf, try this for python:
def values_in(dict, keys):
marker = object()
return [x
for x in (dict.get(k, marker)
for k in keys)
if x is not marker]
- dragonwriter 12y agoFor this case and similar cases where the API provides a "supply your own default value for the failure case" option, this kind of "unique one-time null marker" is actually a very good approach that addresses much of the problem with standard null values. Its not as composable as Option- or list-based approaches, though. (OTOH, you can use it to implement those approaches, and its cleaner for that than the way I did that elsewhere in the thread.)