Python Forum
for loops break when I call the list I'm looping through
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loops break when I call the list I'm looping through
#1
I was trying out the for loops in Python, and I don't fully understand why it breaks.

When I type:

for i in catNames:
    print(catNames[i])
the program breaks.

but when I type:

for i in catNames:
    print(i)
I get the desired result.

I also found out I can do:

for i in range(len(catNames)):
    print(catNames[i])
for the same successful result.

So what is the key difference here? Why does the first option break the program?
Reply
#2
In python for loop iterates over list elements, not their indices. The working one (the second example) should be clear enough to understand that and answer the question yourself.
The last one is considered anti-pattern and should not be not used
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
In the first example the iterable is a list, so i is a list value, not an index. You have no index, so you cannot index. In the third example the iterable returns ints. You can index using an int value.
Reply
#4
'i' tends to suggest an index number, but if you don't have an index number, an integer, or a range of integers, you can't access list elements by index.

Maybe better not to use 'i' in that case, use something like 'name' as it is more "human readable" is the phrase, I believe:

for name in pussyNames:
    print('Here', name, 'here')
But if you need a index number, well, you have already done that!
Reply
#5
Just to mention that if you need (for whatever reason) both index and element/value, the canonical way to do it is using enumerate
names = ['John', 'Jane', 'Marc']
for idx, name in enumerate(names, start=1): # start from 1, default is 0
    print(f'{idx}. {name}')
output
Output:
1. John 2. Jane 3. Marc
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 798 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Break out of nested loops muzikman 11 3,382 Sep-18-2021, 12:59 PM
Last Post: muzikman
  Looping through nested elements and updating the original list Alex_James 3 2,147 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  How to break out of nested loops pace 11 5,411 Mar-03-2021, 06:25 PM
Last Post: pace
  How to create a linked list and call it? loves 12 4,539 Nov-22-2020, 03:50 PM
Last Post: loves
  list call problem in generator function using iteration and recursive calls postta 1 1,922 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Using recursion instead of for loops / list comprehension Drone4four 4 3,157 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,091 Jul-27-2020, 04:19 PM
Last Post: buran
  making list in looping a dictionary glennford49 9 3,597 Jun-25-2020, 03:23 PM
Last Post: ndc85430
  Conditionals, while loops, continue, break (PyBite 102) Drone4four 2 2,993 Jun-04-2020, 12:08 PM
Last Post: Drone4four

Forum Jump:

User Panel Messages

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