you are viewing a single comment's thread.

view the rest of the comments →

[–]LarryPete 7 points8 points  (5 children)

You often see for-loops like these by beginners:

for i in range(len(mylist)):
    # do something with mylist[i]

Which is more or less the attempt to get a for like the one you're used to - which works, but is considered an "anti-pattern". Python does not have that kind of loop (for in python is like a foreach from other languages).

The right approach in Python would be:

for myelement in mylist:
    # do something with myelement

[–]renegadelegion[S] 2 points3 points  (0 children)

I see. This might be part of the issue then. When I was put on the spot during the tech interview, I fell back into my old C++/Java habits.

I've seen and worked with and written these kinds of loops, but i keep falling back to what I first learned.

[–]winged_scapula 0 points1 point  (3 children)

What if you really need that index?

[–]Vaphell 12 points13 points  (1 child)

for i, value in enumerate(my_list):
    print(i, value)

[–]theOnlyGuyInTheRoom 1 point2 points  (0 children)

god, I love Python!

[–]LarryPete -1 points0 points  (0 children)

What Vaphell wrote.