Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an in-place equivalent to 'map' in python?

I have a list of strings that I need to sanitize. I have a method for sanitizing them, so I could just do:

new_list = map(Sanitize, old_list)

but I don't need to keep the old list around. This got me wondering if there's an in-place equivalent to map. Easy enough to write a for loop for it (or a custom in-place map method), but is there anything built in?

like image 574
Herms Avatar asked Nov 10 '10 19:11

Herms


People also ask

What can we use instead of map in Python?

Using Generator Expressions map() returns a map object, which is an iterator that yields items on demand. So, the natural replacement for map() is a generator expression because generator expressions return generator objects, which are also iterators that yield items on demand.

Does Python have a map?

Map in Python is a function that works as an iterator to return a result after applying a function to every item of an iterable (tuple, lists, etc.). It is used when you want to apply a single transformation function to all the iterable elements. The iterable and function are passed as arguments to the map in Python.

Is dictionary in Python same as map?

In Python, dictionaries (or “dicts”, for short) are a central data structure: Dicts store an arbitrary number of objects, each identified by a unique dictionary key. Dictionaries are often also called maps, hashmaps, lookup tables, or associative arrays.

Is map better than for loop Python?

Comparing performance , map() wins! map() works way faster than for loop. Considering the same code above when run in this ide.


2 Answers

The answer is simply: no.

Questions of the form "does XXX exist" never tend to get answered directly when the answer is no, so I figured I'd put it out there.

Most itertools helpers and builtins operate on generic iterators. map, filter, list comprehensions, for--they all work on iterators, not modifying the original container (if any).

Why aren't there any mutating functions in this category? Because there's no generic, universal way to assign values to containers with respect to their keys and values. For example, basic dict iterators (for x in {}) iterate over the keys, and assigning to a dict uses the result of the dict as the parameter to []. Lists, on the other hand, iterate over the values, and assignment uses the implicit index. The underlying consistency isn't there to provide generic functions like this, so there's nothing like this in itertools or in the builtins.

They could provide it as a methods of list and dict, but presently they don't. You'll just need to roll your own.

like image 123
Glenn Maynard Avatar answered Sep 20 '22 15:09

Glenn Maynard


You have to loop:

for i in range(len(old_list)):
    old_list[i] = Sanitize(old_list[i])

There's nothing built-in.

As suggested in the comments: a function as desired by the OP:

def map_in_place(fn, l):
    for i in range(len(l)):
        l[i] = fn(l[i])

map_in_place(Sanitize, old_list)
like image 22
Ned Batchelder Avatar answered Sep 20 '22 15:09

Ned Batchelder