Python Forum
I’m Flat out struggling to understand list indexes - 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: I’m Flat out struggling to understand list indexes (/thread-28390.html)



I’m Flat out struggling to understand list indexes - gr3yali3n - Jul-17-2020

Let’s say
x = [0, 1, [2]] 

x[2][0] = 3

print(x) 
I found this in a book I was scanning through, ok so I’m still trying to understand how this works because it was my understanding that if you used square brackets in this manner the code
Would pull from the 3rd item and the 1st element of said item [2][0] but that doesn’t seem to be what is going on here ,How is x[2][0] = 3 coming out to be [0,1,[3]] ?


RE: I’m Flat out struggling to understand list indexes - menator01 - Jul-17-2020

In the fist list 2nd index, first index of inner list the value 3
if you wish to pull and index you do not do an assignment operation

Output:
>>> x = [0, 1, [2]] >>> x [0, 1, [2]] >>> x[2] [2] >>> x[2][0] 2 >>> x[2][0] = 3 >>> x[2][0] 3



RE: I’m Flat out struggling to understand list indexes - buran - Jul-17-2020

x[2][0] = 3
this is assignment, it does not "access" current value in the list, but assign new value instead


RE: I’m Flat out struggling to understand list indexes - gr3yali3n - Jul-17-2020

I mean I realize that this is an assignment to a certain extent but I guess my problem is understanding how does x[2][0] = 3 , what is the first element of [2]?
When I play with the code : print([2]) vs print([2][0]) the first prints 2 as a list and the second prints 2 as a int, is this the reason for calling [0] of the [2]?


RE: I’m Flat out struggling to understand list indexes - bowlofred - Jul-17-2020

x is a list of 3 elements: [0, 1, [2]]
x[2] is the third element, a list with a single element: [2]
x[2][0] is the first (and only) element from the previous list: 2

So yes, the first element of [2] is the integer "2".

>>> l = [2]
>>> l[0]
2
>>> l.pop(0)
2



RE: I’m Flat out struggling to understand list indexes - menator01 - Jul-17-2020

[0] is the first element of that list. Until it gets reassigned by x[2][0] = 3. Then that element has the value of 3


RE: I’m Flat out struggling to understand list indexes - gr3yali3n - Jul-17-2020

Yes, I see that now, I was playing with idle and it finally hit me, once again my apologies. I’m running on little sleep and constantly chasing a little one around the house, i try to set aside a few hours to study python a day but it usually turns out to be a few minutes here and there to read PDFs ,watch a udemy lecture and practice on my iPhone app.
I really want to thank everyone for being so cool here!


RE: I’m Flat out struggling to understand list indexes - princetonits - Jul-20-2020

Here you can check the visualization for the execution of the code.

Step 1: https://prnt.sc/tjvqbj
Step 2: http://prntscr.com/tjvqjo
Step 3: http://prntscr.com/tjvqok