Very often I process single elements of tuples like this:
size, duration, name = some_external_function()
size = int(size)
duration = float(duration)
name = name.strip().lower()
If some_external_function would return some equally typed tuple I could use map in order to have a (more functional) closed expression:
size, duration, name = map(magic, some_external_function())
Is there something like an element wise map? Something I could run like this:
size, duration, name = map2((int, float, strip), some_external_function())
Update: I know I can use comprehension together with zip, e.g.
size, duration, name = [f(v) for f, v in zip(
(int, float, str.strip), some_external_function())]
-- I'm looking for a 'pythonic' (best: built-in) solution!
To the Python developers:
What about
(size)int, (duration)float, (name)str.strip = some_external_function()
? If I see this in any upcoming Python version, I'll send you a beer :)
foo = SomeExternalData.from_string(*some_external_function()); print(foo.size)…map(as a concept in functional programming) is to apply one function to each element of the list. So, yourmap2won't really be a "true"mapthen.mapbut to have a readable, short closed expression in the end without states, intermediate variables or re-assigning variables.mapis just a way to show what I mean.lzpackage, if you don't want extra dependency I can write a recipe as an answer