Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops in List
#19
i don't use range(len(sequence)) to iterate that sequence.  i think i recall one use of range(len(sequence)) and it was to make an iterator to recreate a similar sequence in a function (it was py2 and i used xrange).  that code would be very different if i re-did it today.  all my code would evolve if i re-did everything every day.  but i don't have enough time for that.  i will re-do a lot of my code, eventually, as i have resolved the last issue preventing me from doing everything in py3 (finally got botocore working correctly in py3 ... a lot of my code uses boto or botocore).

i also do a lot of deeper system stuff and embedded stuff.  but i don't need C for that (or at least not for most of it ... i do for my set-top-box boot loaders) anymore.

(Mar-20-2017, 11:28 AM)snippsat Wrote: The unwritten rule is simple,never use range(len(sequence)).
If need index or manipulate index always enumerate()
Eg:
# Find index of Lemon in list
lst = ['Apple', 'Lemon', 'Orange']
for index, item in enumerate(lst):
    if item == 'Lemon':
        print(f'Lemon is at index: {index}')
Output:
Lemon is at index: 1

why not index=lst.index('Lemon') instead of a loop?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
Loops in List - by SandraDan - Mar-10-2017, 09:20 PM
RE: Loops in List - by wavic - Mar-10-2017, 09:30 PM
RE: Loops in List - by SandraDan - Mar-12-2017, 05:22 PM
RE: Loops in List - by Skaperen - Mar-11-2017, 08:04 AM
RE: Loops in List - by wavic - Mar-11-2017, 08:29 AM
RE: Loops in List - by Skaperen - Mar-12-2017, 04:43 AM
RE: Loops in List - by buran - Mar-11-2017, 08:50 AM
RE: Loops in List - by wavic - Mar-12-2017, 09:07 AM
RE: Loops in List - by ichabod801 - Mar-12-2017, 10:42 AM
RE: Loops in List - by Skaperen - Mar-13-2017, 03:08 AM
RE: Loops in List - by wavic - Mar-12-2017, 05:34 PM
RE: Loops in List - by wavic - Mar-13-2017, 07:42 AM
RE: Loops in List - by Skaperen - Mar-14-2017, 05:57 AM
RE: Loops in List - by ichabod801 - Mar-14-2017, 10:31 AM
RE: Loops in List - by Skaperen - Mar-15-2017, 05:13 AM
RE: Loops in List - by nilamo - Mar-20-2017, 04:07 AM
RE: Loops in List - by Skaperen - Mar-20-2017, 09:30 AM
RE: Loops in List - by snippsat - Mar-20-2017, 11:28 AM
RE: Loops in List - by Skaperen - Mar-21-2017, 02:32 AM
RE: Loops in List - by nilamo - Mar-21-2017, 02:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 952 Sep-18-2023, 07:52 AM
Last Post: buran
  Using recursion instead of for loops / list comprehension Drone4four 4 3,199 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Help with List Loops slackerman73 4 2,766 Nov-25-2019, 07:49 AM
Last Post: perfringo
  Adding items in a list (using loops?) Seneca260 6 2,817 Nov-22-2019, 11:34 AM
Last Post: Seneca260
  simple list check with loops Low_Ki_ 23 15,519 Jan-09-2017, 03:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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