Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: map in place [duplicate]

I was wondering if there is a way to run map on something. The way map works is it takes an iterable and applies a function to each item in that iterable producing a list. Is there a way to have map modify the iterable object itself?

like image 740
johannix Avatar asked Jun 08 '10 19:06

johannix


People also ask

Can map have duplicate keys Python?

Dictionaries in Python First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn't make sense to map a particular key more than once.

Does Set allow duplicates in Python?

Sets cannot contain duplicates. Duplicates are discarded when initializing a set. If adding an element to a set, and that element is already contained in the set, then the set will not change.

Does tuple allow duplicates in Python?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.

What is dict copy ()?

The dict. copy() method returns a shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any change is made in the copied dictionary will also reflect in the original dictionary.


1 Answers

A slice assignment is often ok if you need to modify a list in place

mylist[:] = map(func, mylist)
like image 103
John La Rooy Avatar answered Sep 18 '22 01:09

John La Rooy