How does a For Loop Work?

I need help understanding how For Loops work as I’m working on this mission

Thank you!

2 Likes

Before we look at how a for loop works, Let’s first understand why do we need loops. Imagine you have a list of strings like this: [ '1', '2', '3', '4'...'10'] , and you want to convert those strings into numbers.

Well, we can just do:

simple_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
simple_list[0] = int(simple_list[0])
...
simple_list[9] = int(simple_list[9])

That’s not bad! But what if the list contains 1000 items? This will become too tiring. This is where loop shines! While there are many kinds of loops, we will only discuss for loop here.

Everything in python has a syntax. Syntax helps python interpreter to identify what you wish to do. And one of the available syntax for looping is the for loop syntax:

for <iterator> in <iterable>:
    # do something

Here iterable is anything that is a collection of values. The best examples are a list, tuple, etc. iterator is a variable that stores each value in the iterable 1 at a time. You can name the iterator anything you want row, app, pizza, etc. Just make sure that it is something meaningful considering the values in the iterable. On each iteration, the iterator holds 1 value from the iterable in a sequential fashion. That is on the first iteration it will be first value in the iterable, On second iteration it will be the second value in the iterable and on the last iteration, it will be the last value in the iterable.

The iterable can be accessed outside the loop but it will always contain the last stored value in the loop. Let’s go through a quick for loop example to check it out.

simple_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
for value in simple_list:
  print(int(value))

Output

1
2
3
4
5
6
7
8
9
10

Accessing the iterator outside the loop:

print("value accessed outside for loop: ", value)

Output

value accessed outside for loop:  10
6 Likes

Hi Mary, nice write up.
I had a question with something I am trying to do with a For Loop.
I want to take the contents of a list and create a new variable for each item in the list, not just print it out. So each new iteration outputs a new unique variable with the new list item. Is this a thing or a pipe dream?
Thanks!

I once questioned this to. It is possible (see here) but I suspect what ever you are trying to do would be better off using a dictionary instead.

If you are interested to learn to take better advantage of Python’s best features and improve existing code through a series of code transformations, “When you see this, do that instead.”

You can follow my notes on Transforming Code into Beautiful, Idiomatic Python at nbviewer or github.

You guys were right! The dictionary was exactly what I needed!

Creating the dictionary from the list of directories

for folder in dirlist:
-if folder in dirdic:
–dirdic[folder] += 1
-else:
–dirdic[folder] = 1

Populating the index value with the the full files paths.

for dir in dirdic:
-if dir in dirdic:
–dirdic[dir] = os.path.join(Main_Dir,dir,dir+’_REG.gdb’,‘Final’)

The luck with this was that after the specific directory name, the rest was the same so it could be cookie cutter spliced in.