Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying list elements in a for loop

Tags:

python

I have a list a for which I want to change the elements a[i: j] according to a function f. Can I do better than the naive way?

for index in range(i, j):
    a[index] = f(a)

[By better I mean something closer to map(f, a), or something faster.]

like image 747
Randomblue Avatar asked Sep 14 '26 17:09

Randomblue


1 Answers

You can assign to the slice:

a[i:j] = map(f, a[i:j])
like image 119
Ismail Badawi Avatar answered Sep 17 '26 07:09

Ismail Badawi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!