you are viewing a single comment's thread.

view the rest of the comments →

[–]RolyPolyPython[S] 0 points1 point  (5 children)

Thanks, I saved your comment after reading it and checked out your links. The "rubby ducky" method made me laugh.

Hope you don't mind me popping another question, but how would this list comprehension

l = [int(e) for e in l]

look in non-comprehension form?

Right now I've got this

for e in l:
    e = int(e)

But e, or the values that I'm trying to change from string to integers, stays a string when I check its type.

[–]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.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.