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

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

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

User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

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?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
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: 1790
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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
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
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1710
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests