0

I am new to Python language and used C++ for the previous 2 years. I am trying to understand why these two codes produce different results:

The first for loop:

l1=[1,2,3,4,5]
for i in l1:
    i+=10

print l1
# [1,2,3,4,5]

The second for loop:

l1=[1,2,3,4,5]

for i in range(len(l1)):
    l1[i]+=10

print l1
# [11,12,13,14,15]

Why doesn't the first loop produce the same result? I thought that the "for i in object" does the dereferencing for you and gives you access to the object's value?

1
  • 1
    You're not storing the value back to the array in the first loop, just using it. Commented Jan 15, 2014 at 15:16

4 Answers 4

1

In the first example you are altering the list at all, instead you are iteratng through it and you are altering the i variable you made

however, in the second example you index li so you are directly acsessing its items, instead of a variable assigned the the items value

Sign up to request clarification or add additional context in comments.

3 Comments

So I can think of i as a pointer that takes the value of the item in list each time?
yes, i takes the value of the item at the current iteration/position, but i isn't the actual item in the list it just has its value in, but in the second one that's the actual item in the list because you are indexing it straight from the list :)
@Reboot_87 No problem!, if it answered you're question you can accept my answer! Good luck to the rest of your learning :)
1

The first example doesn't give the same results as the second because you're only operating on the value that's stored in the list, not on the list element itself. When you use the slice notation l1[i]+=10 you are performing an operation on the list element, and changing the values stored in the list.

5 Comments

What do you mean when you say: "value that's stored in the list"? what is the different between that and the list element itself?
When you used l1 as an iterator to assign to the variable i, the value that was referenced in the list now becomes an integer value referenced by i, and is separated from the list reference. Does that make sense?
Yes! please correct me if I am wrong. (i) is a pointer that takes the value of the item in the list every iteration?
@Reboot_87 I suppose it could be thought of as that, insofar as I understand pointers (my knowledge of C is hazy on the best of days). But, if some other process were to change the list value, i would not be changed. So, say you have for i in l1:, then do l1[0] = 42, then print the value of i, i will still be 1, even though the list element changed. Hopefully I'm not further confusing you :)
Not at all. I understand it now. (i) doesn't access the object in the list itself, just references its value
0

i in first loop is a scalar value and is copied from l1, however l1[i] in second loop directly modifies data in array.

Comments

0

1st for loop: i is the iteration variable that is taking value from the l1 in each iteration. In the first iteration i is 1. Inside the loop and after the

i+=10

i will be equal to 11.

At the second iteration i is given the value 2 (2nd value on the l1 list) that replaces value 11. and so on... The list is not modified that way.

2nd for loop: Again i takes value from the values inside l1. Now you are using:

l1[i]+=10

Tha actually modifies the list content.

That explains the different output in the second for loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.