The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of
@flyingsquirrel Yup. And there's a multiprocessing version which can be useful if you're doing a very large csv but you probably want imap() for that one.
Source code: Lib/multiprocessing/ Availability: not Android, not iOS, not WASI. This module is not supported on mobile platforms or WebAssembly platforms. Introduction: multiprocessing is a package...
If you're looking for a function that applies another function to each element of a list and aggregates the results, it'd typically be done with a list comprehension:
[your_function(item) for item in your_list]
But you can also use list(map(your_function, your_list)) if you want. Basically, it's a built-in function rather than a method of the list class.
There's also a whole discussion to be had about lists vs generators and why you often wouldn't even need to make a list in the first place, but I won't get into that unless you want to know more.
the logic being that `map()` take any iterable, jot only lists, but sets, dicts, and anything that complies with the iterable protocol. I'm not saying you have to _like_ it. See also `str.join()`.
@Marcos Dione Yeah, in hindsight, that makes sense. I'm usually a Haskell programmer, not a Python programmer, so I don't usually have to deal with the question of whether a function is on an object or not.
I understand. I get similar issues when I jump to other languages. The ones I had more impedance has been golang for certain decisions and rust because of the "harsh" syntax and verboseness.
Jonathan Lamothe
in reply to Jonathan Lamothe • •Also, do lists in #Python seriously not have a
.map
function?Edit: Ohhh... I expected it to be a method on the list object itself.
Tech Cyborg reshared this.
gobsmacked
in reply to Jonathan Lamothe • • •Python map() function
GeeksforGeeksJonathan Lamothe likes this.
Darcy Casselman
in reply to Jonathan Lamothe • • •Jonathan Lamothe likes this.
Darcy Casselman
in reply to Darcy Casselman • • •Scott Williams 🐧
in reply to Darcy Casselman • • •@flyingsquirrel Yup. And there's a multiprocessing version which can be useful if you're doing a very large csv but you probably want imap() for that one.
docs.python.org/3/library/mult…
multiprocessing — Process-based parallelism
Python documentationDavid Zaslavsky
in reply to Jonathan Lamothe • • •If you're looking for a function that applies another function to each element of a list and aggregates the results, it'd typically be done with a list comprehension:
[your_function(item) for item in your_list]
But you can also use list(map(your_function, your_list)) if you want. Basically, it's a built-in function rather than a method of the list class.
There's also a whole discussion to be had about lists vs generators and why you often wouldn't even need to make a list in the first place, but I won't get into that unless you want to know more.
#Python
Jonathan Lamothe likes this.
Marcos Dione
in reply to Jonathan Lamothe • • •Jonathan Lamothe
in reply to Marcos Dione • •Marcos Dione
in reply to Jonathan Lamothe • • •