Python Forum

Full Version: Deeper explanation of the "for i" please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
for i in a:
if i < 5:
b.append(i)


MY questions is why does the "i" in the "for i in a" part of the code stand for the each of the indexes of the list and cycles through it, but in the b.append(i) it uses the value in that index slot instead of the index position. Any help is appreciated and ill try and provide any clarification that I can.
It doesn't stand for the index.  for i in {enumerable} always iterates over items, not indices.  If you do want the index, the built-in function enumerate() can get that.
>>> things = list(range(0, -10, -1))
>>> things
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> for i in things:
...   print(i)
...
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
>>> for index, i in enumerate(things):
...   print("{0} => {1}".format(index, i))
...
0 => 0
1 => -1
2 => -2
3 => -3
4 => -4
5 => -5
6 => -6
7 => -7
8 => -8
9 => -9
I'm more of trying to understand why python does certain things in this question rather than looking for better ways to do things.

So i guess a different way of asking my question would be if:
a = [8,7,6,5,4,3,2,1]
for i in a:
    if i < 5:
    b.append(i)
why does it put the values of "a" into b instead of the slot numbers? For example why does it but 8,7,6,5,4,3,2,and 1 instead of 1,2,3,4,5,6,7, and 8?
I presume you are referring to index's when you say slots. If not, you should. 

list =    1, 2, 3, 4
index = 0, 1, 2, 3

list =    A, B, C, D
index = 0, 1, 2, 3

@nilamo's explanation of the difference between the value of a lists item and it's index is spot on. Remember though that index's start with 0, not 1
actually this is THE better way, the pythonic way. Let's look at these 2 code snippets.
fruits = ['apple', 'orange', 'peach', 'apricot', 'plum']
for fruit in fruits:
    print(fruit)
fruits = ['apple', 'orange', 'peach', 'apricot', 'plum']
for i in range(5):
    print(fruits[i])
 
both snippets yield the same result. How do you think - which one is better? The first one is almost like a natural language...that also comes to emphasize the importance of using descriptive names, not single-char names like a and b
(Oct-06-2017, 07:36 PM)mtndewlove Wrote: [ -> ]I'm more of trying to understand why python does certain things in this question rather than looking for better ways to do things.

So i guess a different way of asking my question would be if:
a = [8,7,6,5,4,3,2,1]
for i in a:
    if i < 5:
        b.append(i)
why does it put the values of "a" into b instead of the slot numbers? For example why does it but 8,7,6,5,4,3,2,and 1 instead of 1,2,3,4,5,6,7, and 8?

I'm not sure if you've actually tried that... i would be exactly the same both inside the loop, and inside b.

>>> a = [8, 7, 6, 5, 4, 3, 2, 1]
>>> b = []
>>> for i in a:
...   print(i)
...   if i < 5:
...     print("appending: {0}".format(i))
...     b.append(i)
...
8
7
6
5
4
appending: 4
3
appending: 3
2
appending: 2
1
appending: 1
>>> b
[4, 3, 2, 1]
Hm, i think i see the er in my question. I get what you are all saying and in not disputing anything of what anyone says. I'm just trying to understand what is it in the "for ___" code that makes it run through all the indexes in the list?