[Beginner] Never use "for i in range(len(sequence)):"

A place where you can post Python-related tutorials you made yourself, or links to tutorials made by others.

[Beginner] Never use "for i in range(len(sequence)):"

Postby Mekire » Sat Jan 11, 2014 7:48 am

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:
Code: Select all
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:
Code: Select all
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:
Code: Select all
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:
Code: Select all
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 link for more info on how to iterate effectively.
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Beginner] Never use "for i in range(len(sequence)):"

Postby micseydel » Sat Jan 11, 2014 8:22 am

There's also when you want to iterate over two or things in parallel, should this cover zip() as well?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1367
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Beginner] Never use "for i in range(len(sequence)):"

Postby stranac » Sat Jan 11, 2014 12:41 pm

When I want people to actually understand how they should iterate stuff, I usually link them to http://bit.ly/pyiter
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: [Beginner] Never use "for i in range(len(sequence)):"

Postby micseydel » Sat Jan 11, 2014 1:15 pm

Awesome stranac! I didn't have as much justification for why enumerate() was better than xrange(len()) when only the index was needed. Generally awesome tutorial too.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1367
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Beginner] Never use "for i in range(len(sequence)):"

Postby Mekire » Sat Jan 11, 2014 2:16 pm

@micseydel: Basically I just wanted something simple to link people to that I found doing this (I literally typed the same thing out in various places 4 times in a week recently). This is extremely short though so if you feel something on zip would be an appropriate addition you are welcome to add it in.

@stranac: That is indeed an excellent link and I'll be sure to remember it. I will add it at the bottom for any that want more detail.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest