Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
double loop
#1
Hello,

I recently started with learning Python and don't fully understand how a nested loop works.

Below I have 2 similar codes with the only difference that I create in the first code an empty list 'int_fields' before the loop and in the second code I put an empty list 'int fields' within the loop.

I see the difference in output, but I don't know how the double loop causes the difference in output. Could somebody please give some clarification?


string_list = ['1994,1,1,6,8096', '1994,1,2,7,7772', '1994,1,3,1,10142']
final_list = []
int_fields = []
for data in string_list:
    string_fields = data.split(",")
    for value in string_fields:
        intvalue = int(value)
        int_fields.append(intvalue)
    final_list.append(int_fields)
print(final_list)
Output:
[[1994, 1, 1, 6, 8096, 1994, 1, 2, 7, 7772, 1994, 1, 3, 1, 10142], [1994, 1, 1, 6, 8096, 1994, 1, 2, 7, 7772, 1994, 1, 3, 1, 10142], [1994, 1, 1, 6, 8096, 1994, 1, 2, 7, 7772, 1994, 1, 3, 1, 10142]]
string_list = ['1994,1,1,6,8096', '1994,1,2,7,7772', '1994,1,3,1,10142']
final_list = []
for data in string_list:
    int_fields = []
    string_fields = data.split(",")
    for value in string_fields:
        intvalue = int(value)
        int_fields.append(intvalue)
    final_list.append(int_fields)
final_list
Output:
[[1994, 1, 1, 6, 8096], [1994, 1, 2, 7, 7772], [1994, 1, 3, 1, 10142]]
Thanks,
Reply
#2
In the first case, there is only one int_fields list, that you add to final_list three times. In the second case, there are three different int_fields that are unrelated, and each get added to final_list.

Here's a little test to try to hopefully help out:
>>> items = []
>>> for outer in range(0, 5):
...   print(f"Outer loop: {outer}")
...   for inner in range(0, 3):
...     print(f"Inner loop: {inner}")
...     items.append(inner)
...     print(f"Current items: {items}")
...   print("")
...
Outer loop: 0
Inner loop: 0
Current items: [0]
Inner loop: 1
Current items: [0, 1]
Inner loop: 2
Current items: [0, 1, 2]

Outer loop: 1
Inner loop: 0
Current items: [0, 1, 2, 0]
Inner loop: 1
Current items: [0, 1, 2, 0, 1]
Inner loop: 2
Current items: [0, 1, 2, 0, 1, 2]

Outer loop: 2
Inner loop: 0
Current items: [0, 1, 2, 0, 1, 2, 0]
Inner loop: 1
Current items: [0, 1, 2, 0, 1, 2, 0, 1]
Inner loop: 2
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2]

Outer loop: 3
Inner loop: 0
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0]
Inner loop: 1
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1]
Inner loop: 2
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]

Outer loop: 4
Inner loop: 0
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0]
Inner loop: 1
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1]
Inner loop: 2
Current items: [0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]
Reply
#3
To elaborate on the one versus three lists Nilamo mentioned, it's all about the instantiation of "int_fields". When your code does this:

outsideLoop = []
for x in range(10):
    outsideLoop.append(x)
    print(outsideLoop)
The list is created outside of the loop and never resets. So, every number is appended to the same list. Conversely, if we do this:

for x in range(10):
    insideLoop = []
    insideLoop.append(x)
    print(insideLoop)
The list is instantiated with each iteration of the loop. So, each time the loop iterates, it creates a new list named "insideLoop".

Now, there is one more thing that you've stumbled upon. A list in Python is a reference type. That means that every instance of it refers to the same memory location. For instance, in the following code, we append a list named "test" to another list named "test2" and print test2 each time. So, test2 = [test, test, test] by the end; notice what happens:

test = []
test2 = []
for x in range(3):
    for x in range(3):
        test.append(x)
        test2.append(test)
print(test2)

Output:
[[0, 1, 2]]
[[0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2]]
[[0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]]
Each time we append to test, all copies of it get updated, not just the most recent one. This is because test2 truly contains [<list at memory location abc>, <list at memory location abc>, <list at memory location abc>].
Reply
#4
Much appreciated both, fully understand now! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: list comprehension for loop with double if statement mart79 3 2,465 May-04-2020, 06:34 AM
Last Post: buran
  Double for loop with dates in array leifeng 1 1,645 Apr-05-2020, 03:27 PM
Last Post: leifeng
  loop through list or double loop 3Pinter 4 3,482 Dec-05-2018, 06:17 AM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

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