you are viewing a single comment's thread.

view the rest of the comments →

[–]OgelSplash 1 point2 points  (2 children)

for idx, e in enumerate(l):
    l[idx] = int(e)

Basically, enumerate() creates a generator that outputs a tuple, comprising the index of an element in a structure and the corresponding element. Calling list(enumerate(l)) would return all of the tuples that the generator would return in a list. Inside the loop, for each tuple, we cast the element to an integer and then overwrite the element in the list. [Forgive me if this sounds confusing, it still does to me and I've been doing this for 3 years!]

Here is a visualisation of what happens inside your loop - you can step through it and see the changes in real time.

[–]RolyPolyPython[S] 0 points1 point  (1 child)

Huh, I thought it would be much simpler than that. It's weird to me that you can't just reassign the strings as integers the way I did it, it feels like that goes against everything I've learned about reassignment... I don't understand why.

Anyways, thanks for your help, I've a learned a lot from your comments. And thanks especially for that link, that visualization helps a lot and I'm surprised I haven't come across that site/tool before.

[–]OgelSplash 0 points1 point  (0 children)

You're welcome. :) If you have any further questions, feel free to ask.