This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Running a generator in a map-like manner
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: nullfact0r, remi.lapeyre, rhettinger
Priority: normal Keywords:

Created on 2020-06-24 19:44 by nullfact0r, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg372275 - (view) Author: Natsumi (nullfact0r) * Date: 2020-06-24 19:44
I suggest adding a function which behaves like map but without returning anything to iterate over a generator.

This is useful in cases where you need to run a function on every element in a list without unnecessarily creating a generator object like map would. 

I think given the existence of the map function that this should be added to Python.
msg372276 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-24 20:09
If I'm understanding correctly you want:

def xmap(f, iterable):
    for e in iterable:
        f(e)


Is that correct?
msg372278 - (view) Author: Natsumi (nullfact0r) * Date: 2020-06-24 20:14
Exactly that was the plan!
msg372279 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-06-24 20:18
I don't think something so obvious can be added, if you want a one liner you can write:

for e in iterable: f(e)

I think it would go in itertools if it were accepted.
msg372280 - (view) Author: Natsumi (nullfact0r) * Date: 2020-06-24 20:22
If it won't be added do you reckon creating a library to solve this issue would be appropriate?
msg372285 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-06-24 21:53
FWIW, you can already do this with map() and deque():

   deque(map(f, it), maxsize=0)
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85279
2020-06-24 21:53:54rhettingersetmessages: + msg372285
2020-06-24 20:22:15nullfact0rsetmessages: + msg372280
2020-06-24 20:18:41remi.lapeyresetnosy: + rhettinger
messages: + msg372279
2020-06-24 20:14:42nullfact0rsetmessages: + msg372278
2020-06-24 20:09:09remi.lapeyresetcomponents: + Library (Lib), - Interpreter Core
2020-06-24 20:09:00remi.lapeyresetnosy: + remi.lapeyre
messages: + msg372276
2020-06-24 19:44:38nullfact0rcreate