+ 3
Replacing List Items
Replacing items using for-in loop is weird. And yes, strings have a replace function, but there seems to be nothing like that for list. https://code.sololearn.com/cJV8gll86QYe/?ref=app Why is this the behaviour? Do you have better work-arounds?
7 Respostas
+ 2
But i is a different variable, how would assigning a value to it affect the list ?
The following line,
for i in list, assigns next value in list to variable i on each iteration.
+ 1
Yes, I know, I just always assumed that it represents the actual object in the list.
And why is there no replace function for list? Strings are immutable, but there is a replace method(even if it just creates a new string). List are mutable, but there is no replace method, at least one that is easy to implement in a for-in loop...
Long rant, probably pointless, but it would be nice if the for-in replacement worked
+ 1
Bob_Li yes there isn't and i can't answer why so ,but here are the following ways to replace when using for in loop.
a=[1,2,3,4]
for i in range(len(a)):
if a[i]==2:
a[i]=4
print(a)
------------------------------------------
a=[1,2,3,4]
a=[4 if i==2 else i for i in a]
print(a)
------------------------------------------
a=[1,2,3,4]
for i,j in enumerate(a):
if j==2:
a[i]=4
print(a)
list comprehension looks easy to me but i can't say which is the most efficient .But if one wants to know they can measure the cpu time using timeit library on large numbers list.
+ 1
Yes, and those are long and hard, comparatively.
Speed is not everything.Simplicity is one of Python's goal. So it looks like an oversight by the devs. Unless there is a good reason why it's not implemented.
+ 1
Yep, it cannot be done this easily.
Stackoverflow discussion:
https://stackoverflow.com/questions/20688324/JUMP_LINK__&&__python__&&__JUMP_LINK-assign-values-to-list-elements-in-loop
But what if someone could figure out how to make i behave more than a local variable, but really like:
i = list[list.index(i)]
so that:
😇 i = new_value😇
internally behaves like:
😈 list[list.index(i)] = new_value😈
when it is assigned a new value in the loop?
I mean, look at it!
The bottom expression is nasty.
But it works.
It only needs to do this if it is assigned a new value. Then i behaves normally again.
Wouldn't that work?
Maybe someone with a deeper understanding of Python's internal workings could explain how this would be hard or even impossible.
I mean, it works if I write it that way...
Sorry for the long rant, but it's like looking at a bad design and uneccesary complication.
Python is open source, if I had the skillset, I would take a look at it, but it's beyond me..
0
Ok, still beating the dead horse..
Here's some interesting reading about why Python's for loops are different.
https://opensource.com/article/18/3/loop-better-deeper-look-iteration-python
deeper and deeper we go into the rabbit hole.
I hope you are learning a lot from this. I know I am.
0
Ok, I'm done with this post.
Here is the ultimate way of the true Python jedi 🤓:
https://code.sololearn.com/cTP47K41m8kl/?ref=app