Python Forum
I’m Flat out struggling to understand list indexes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I’m Flat out struggling to understand list indexes
Let’s say
x = [0, 1, [2]] 

x[2][0] = 3

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]] ?
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

>>> x = [0, 1, [2]] >>> x [0, 1, [2]] >>> x[2] [2] >>> x[2][0] 2 >>> x[2][0] = 3 >>> x[2][0] 3
x[2][0] = 3
this is assignment, it does not "access" current value in the list, but assign new value instead
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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]?
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]
>>> l.pop(0)
[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
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!
Here you can check the visualization for the execution of the code.

Step 1:
Step 2:
Step 3:

Possibly Related Threads…
Thread Author Replies Views Last Post
  Struggling for the past hour to define function and call it back godlyredwall 2 266 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  struggling with != statements CallumRoberts2004 2 271 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  How to print array indexes? Mark17 6 584 Aug-03-2020, 04:26 PM
Last Post: Mark17
  Struggling with nested list gr3yali3n 3 443 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Understand list comprehension and draw PUP280 9 907 Apr-23-2020, 05:25 PM
Last Post: PUP280
  Struggling to exit this while loop fatherted99 5 566 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  Struggling with several while loops nsadams87xx 1 436 Nov-25-2019, 02:12 AM
Last Post: Larz60+
  struggling with loop/webscrape zarize 4 621 Sep-27-2019, 08:44 AM
Last Post: zarize
  Still struggling with np.where... pberrett 1 572 May-10-2019, 11:30 AM
Last Post: scidam
  From flat to nested structure CptHaddock 5 2,984 Mar-21-2018, 07:50 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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