Hi Community!!

I’m having some troubling understanding “For Loops.” I want to understand it well just like everything so far. For some reason, the concept just isn’t clicking together just yet. So I welcome any suggestions on how I can better understand, what you all did to understand this concept.

Thank you again.

For loops are used to select every element of an iterable (list, generators, dictionary etc) one after the other to perform a certain operation on them.

For example, say we have a list [1, 2, 3, 4, 5, 6, 7] and we want to add 1 to each number. We can use a for loop to get every member of this list and increase it by one. We can chose to put these new numbers is a new list of in the old list.

``````a =  [1, 2, 3, 4, 5, 6, 7]

for idx, num in enumerate(a):
a[idx] = num + 1

print(a)

Output:
[2, 3, 4, 5, 6, 7, 8]
``````

This is the same operation can be done using a list comprehension:

``````[i + 1 for i in a]
``````

You can use for loops also with set, generator, tuple, and dictionary comprehension:

``````a =  [1, 2, 3, 4, 5, 6, 7]

set(value + 1 for key, value in enumerate(a))

Output:
{2, 3, 4, 5, 6, 7, 8}
``````
1 Like

Hi @amouzout,

It would be great if you had any example code to share which you didn’t understand. Though let me try to explain.

when you write

``````for xyz in something:
``````

`xyz` is the iterating variable and it can take any names, like all other variables.
`something` is the bunch of values through which we are going to run through one by one. So this `something` has to be a list, dictionary, a range of number etc that can contain a bunch of values.

Now when we say `for xyz in something` in the backend, python gets ready to run a loop through each value stored in `something`

In the first iteration, the variable `xyz` gets the first value present in `something`
in the second iteration the value stored in `xyz` gets overwritten and the second value present in `something` gets assigned.

This process or the loop continues till all the values in `something` is used up.

Now let us look at a simple example.

``````for x in [1,2,3]:
print (x)
``````

This will print
1
2
3

The same can be written as

``````something = [1,2,3]
for xyz in something:
print(xyz)
``````

This will also print
1
2
3

Now this can also be

``````for i in range(1,4):
print(i)
``````

Now if this can be extended to anything, not only to numbers

``````for fruits in ['apple', 'orange', 'mango']:
print(fruits)
``````

will print
apple
orange
mango

This can also be

``````something = ['apple', 'orange', 'mango']
for xyz in something:
print(xyz)
``````

This will also print the same output
apple
orange
mango

But when we do coding we need to give very intuitive names to our variables so that people can understand it easily.

So instead of ` for xyz in something` we try to give names like `for fruit in fruits`
or `for app in apps_data` or ` for numbers in range(1,45)` etc

So this is the basics of for loop. This can be extended to string, dictionaries etc. But mostly the underlying principles are quite similar.

For string

``````for letter in "character":
print(letter)
``````

It will print
c
h
a
r
a
c
t
e
r

To iterate through dictionaries are a bit different. Maybe once you figure out these concepts, it will be easier for you to understand for loop in dictionaries.

I hope this helps.

Now when it comes to naming these variables, you can use any names like I have suggested earlier.

2 Likes