Python Forum
in tutorials: Never use "for i in range(len(sequence)):
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
in tutorials: Never use "for i in range(len(sequence)):
#1
i just saw this tutorial.  i have had a couple cases where i needed the index in the loop, or the index of where i broke out of the loop.

i also made macros to step through lists and dictionaries directly and made extensive use of them. but that was back in my assembler days. i also made functions like that in C and used those a lot.  my AVL code in C has like functions (to step forward or backwards in a tree/mapping/dictionary).

one thing i like in python is that one can do things either way. but i have not, yet, run into a need to use goto in python. where would i go, anyway? the only use of goto i every made in C was to go to error handlers/cleanups at the end of function. python has exception handling, instead.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Quote:i have had a couple cases where i needed the index in the loop, or the index of where i broke out of the loop.

The point isn't that you never need the index, but that you are much more likely to need the object itself.  In cases where you do need the index, enumerate is almost always the best tool as it gives access to the object and the index.  If you need the index because you are trying to iterate over two sequences in parallel, you should probably be using zip instead.

Quote:the only use of goto i every made in C was to go to error handlers/cleanups at the end of function. python has exception handling, instead.
And exceptions can actually be abused quite badly to this end as well; though at least you can't arbitrarily jump back in time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 3,112 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Zlib tutorials Vwjap 2 2,342 Mar-13-2021, 09:22 PM
Last Post: Vwjap
  Hi, looking for "dunder" tutorials. AlluminumFoil 3 1,984 Mar-02-2020, 08:37 PM
Last Post: jefsummers
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 6,879 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  I do not understand why my python looks different from tutorials. noodlespinbot 2 5,044 Oct-12-2019, 09:56 PM
Last Post: noodlespinbot
  Python Modules and Tutorials marienbad 1 2,185 Jan-31-2019, 08:36 PM
Last Post: ichabod801
  mpi4py examples/tutorials MuntyScruntfundle 1 2,652 Dec-01-2018, 10:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020