Bottom Page

Thread Rating:
  • 4 Vote(s) - 3.25 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Basic] Never use "for i in range(len(sequence)):"
#1
This will be pretty short and to the point, but I find my self typing this out far too often these days and want something to link to.

Most languages around today have some type of for loop syntax.  Python is no different; in fact, in python for loops are highly preferred in most circumstances over while loops.

In languages like C one loops like this:
for (i=0; i<length_of_sequence; i++)
In the above we are getting indexes from our for loop.

Many Python beginners, whether they be coming from other languages like C, or brand new to programming, inevitably go through a phase where they try to do the same thing.

This results in things like this:
for i in range(len(sequence)):
    print(sequence[i])
This is never the right answer.  In python we have the power and ability to loop directly over the items in a sequence.

We can instead do this:
for item in sequence:
    print(item)
It is simpler to type; simpler to read; and most importantly makes more sense.  As we were never concerned with the index to begin with, we don't need to bother with it.

There are however cases in which one does want the index as well as the item.  In such cases enumerate is the tool of choice.

Our loop becomes:
for i,item in enumerate(sequence):
    print("{} : {}".format(i,item)
In short, if you ever catch yourself writing range(len(sequence)) rethink your goal and figure out what it really is you are interested in.

See this great article from Ned Batchelder for more info on how to iterate effectively.
-Mek
ljmetzger, micseydel, metulburr like this post
#2
This message left intentionally blank.
nilamo likes this post
#3
maybe add a zip in a for loop example as questioned here
Quote: for i in correct:
#how could i compare here the two array without writing it in the C way?
for item, check in zip(received, correct):
    if item == check:
        coincident += 1
#4
(Sep-18-2017, 11:38 AM)metulburr Wrote: maybe add a zip in a for loop example as questioned here
Quote:for i in correct:
#how could i compare here the two array without writing it in the C way?
for item, check in zip(received, correct):
    if item == check:
        coincident += 1
 

 Wouldn't it be better to avoid an intermediate variable or zipping, and instead just do set arithmetic?
coincident = len(set(received) & set(correct))
Unless one or both of those are a generator/stream to something that might not all fit into memory, I suppose.
#5
nilamo, that doesn't work when the order of elements in received and correct is important, which was the main point in this case :-)
#6
I think more he was asking if a section on zip should be added here to teach about iterating over lists in parallel; not necessarily specific to that post.

I do believe however that this type of iteration is mentioned in the Batchelder link I included at the end of the post.
metulburr likes this post

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)