How iteration variable is appending the values to list?

Screen Link:

My Code:

apps_data=[[‘Call of Duty’,5.0],[‘Facebook’,0.0]]

for app in apps_data:
price = app[1]

if price == 0:
    app.append("free")
if price !=0:
    app.append("Non Free")

print(apps_data)

What I expected to happen:
I am unable to understand the code.
How the iteration variable is appen the value to list

Please make sure that you are following the guidelines as mentioned in this post - Guidelines for asking a technical question in our Community. Especially guidelines 1 and 5.

Here is an example to help explain:

things = [[thingA, 7], [thingB, 0]]` # Line 1

for thing in things: # Line 2
    cost = thing[1] # Line 3
    if cost == 0: # Line 4
        thing.append("No cost") # Line 5
    if cost != 0: # Line 6
        thing.append("Costs money") # Line 7
print(things) # Line 8

Line 1:

  • Creates a list of lists called ‘things’

Line 2:

  • Kicks of a for loop that goes through each sub list in things
  • First time through thing = things[0]
    ** thing = [thingA, 7]
  • Second time through thing = things[1]
    ** thing=[thingB, 0]

Line 3:

  • Since it is the first time through cost=7

Line 4:

  • Since this is false we skip line 5 and go to line 6

Line 6:

  • Since this is true we go to line 7

Line 7:

  • This will append (add to the end of a list) “Costs money”
  • Since we are in the for loop this is appended to the current sublist stored as thing not the main list of lists.
  • thing = [thingA, 7, "Costs money"]

Now we return to line 3, but have a new value for thing. Once we have gone through all of the sublists in things we exit the for loop and go to…

Line 8:

  • things = [[thingA, 7, “Costs money”], [thingB, 0, “No cost”]]`

Hope this helps!

Hi emilywhite,

Yes,I have a better understanding now, still need few more clarification, the # line 5 and # line 7 are appending the value to thing list not things but when we do print(things) in the #line 8, how it is printing this output …[[thingA, 7, “Costs money”], [thingB, 0, “No cost”]]

Please help me to understand.

Hi silambucsem,

What did you expect the print statement to be?

Keep in mind that append is a method and methods are attached to objects and can change their state. We write thing.append("Costs money") because the method is acting on the list object and can change it. “Costs money” is the parameter.

This is different from functions that use objects as parameters (e.g., len(thing)) which use the object as an input parameter and return a result that doesn’t change the object. We don’t write thing.len() because len doesn’t do anything to the thing object .

I am trying to understand the below line,

Line 7:

  • This will append (add to the end of a list) “Costs money” - Understood
  • Since we are in the for loop this is appended to the current sublist stored as thing not the main list of lists. - Not understood
  • thing = [thingA, 7, "Costs money"] - Understood

Here, thing hold sublist of things and has a appended value thing = [thingA, 7, "Costs money"] and [thingB, 0, “No cost”] in each iteration.

I am trying to understand, how appending the values to thing reflected in things when we print(things).
If my understanding is correct, # Line 8 is printing the output from # Line 2 not from # line 1 ?

I am completely new to python, so please don’t ignore my stupid questions.

I think these are good questions!

Sometimes I like to write out how the variables change as I go through a loop.

Line 1

  • things: [[thingA, 7], [thingB, 0]]

Line 2

  • things: [[thingA, 7], [thingB, 0]]
  • thing: [thingA, 7]

Line 3

  • things: [[thingA, 7], [thingB, 0]]
  • thing: [thingA, 7]
  • cost: 7

Line 4

  • things: [[thingA, 7], [thingB, 0]]
  • thing: [thingA, 7]
  • cost: 7

Line 6

  • things: [[thingA, 7], [thingB, 0]]
  • thing: [thingA, 7]
  • cost: 7

Line 7

  • things: [[thingA, 7, "Costs money"], [thingB, 0]]
  • thing: [thingA, 7, "Costs money"]
  • cost: 7

Line 2 (Going through for loop for next item in things. Cost only exists for 1 iteration of the loop)

  • things: [[thingA, 7, "Costs money"], [thingB, 0]]
  • thing: [thingB, 0]

Line 3

  • things: [[thingA, 7, "Costs money"], [thingB, 0]]
  • thing: [thingB, 0]
  • cost: 0

Line 4

  • things: [[thingA, 7, "Costs money"], [thingB, 0]]
  • thing: [thingB, 0]
  • cost: 0

Line 5

  • things: [[thingA, 7, "Costs money"], [thingB, 0, "No cost"]]
  • thing: [thingB, 0, "No cost"]
  • cost: 0

Line 6

  • things: [[thingA, 7, "Costs money"], [thingB, 0, "No cost"]]
  • thing: [thingB, 0, "No cost"]
  • cost: 0

Line 8 (Went through all items in for loop. Thing and cost belong to the loop. Since things exists outside the loop the changes are kept.)

  • things: [[thingA, 7, "Costs money"], [thingB, 0, "No cost"]]

Here is how I think about:
Even though we don’t have to write the code this way in the background Python treats the things list like this…

list1 = ["thingA", 7]
list2 = ["thingB", 0]
things = [list1, list2]

In the for loop python isn’t really doing anything to things since it is a list holding pointers to where other lists are stored.

The other tricky idea is that append mutates (changes) list objects. It is a little different then some other things in Python where you need to reassign values to a variable. I’ll use >> to indicate output. Like…

x = 1
x + 100
>>101
print(x)
>>1
x += 100
print(x)
>>101

So if you try some code like what is below the lists will change without having to do any variable assignment.

my_list = [1, "cat", ["a", "b"]]
my_list.append("emily")
print(my_list)
>>[1, "cat", ["a", "b"], "emily"]
my_list[2].append("c")
print(my_list)
[1, "cat", ["a", "b", "c"], "emily"]

I hope this helps! If you still have questins I can try again :slightly_smiling_face:

Thank you for your step by step explanation. This is absolutely clear and understandable.

1 Like