Python Forum
Deeper explanation of the "for i" please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deeper explanation of the "for i" please
#1
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.
Reply
#2
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
Reply
#3
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?
Reply
#4
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
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
Reply
#6
(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]
Reply
#7
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 408 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  A better explanation of the last post Led_Zeppelin 9 2,403 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 2,053 Jul-31-2021, 11:05 AM
Last Post: Sherine
  Explanation of except ... as : Fernando_7obink 2 1,946 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 2,977 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,253 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  Explanation of the left side of this statement please rascalsailor 3 2,517 Sep-09-2020, 02:02 PM
Last Post: rascalsailor
  Need explanation of one line of code Fliberty 6 3,502 Feb-18-2020, 12:50 AM
Last Post: Fliberty
  explanation of code hikerguy62 2 2,262 Aug-01-2019, 01:37 PM
Last Post: hikerguy62
  While loop explanation rdgbl 1 2,309 Dec-18-2018, 01:03 AM
Last Post: stullis

Forum Jump:

User Panel Messages

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