double loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: double loop (/thread-13542.html) |
double loop - PolskaYBZ - Oct-19-2018 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)
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 Thanks,
RE: double loop - nilamo - Oct-19-2018 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] RE: double loop - stullis - Oct-20-2018 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>]. RE: double loop - PolskaYBZ - Oct-20-2018 Much appreciated both, fully understand now! :) |