3 ms·
Not sure if there is a general place to learn this stuff (like a specific tutorial) but when hacking in python, it helps to think not in terms of c like loops i
by noobermin 9y ago
Not sure if there is a general place to learn this stuff (like a specific tutorial) but when hacking in python, it helps to think not in terms of c like loops in which you iterate over a collection using a counter like `for(int i=start; i < end; ++i) {...`, instead, for loops in python are specifically for iterating over collections.
>>> a=[1,2,3,4]
Instead of
>>> for i in range(len(a)):
... print("->{}".format(a[i]))
You can (should!) just do
>>> for x in a:
... print("->{}".format(x))
The entire point of for loops in python is it is for iterating over collections. Even better is that very same syntax can be use if `a` is not a list that can be indexed, like a generator, for example.