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
#1
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]] ?
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#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

Reply
#4
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]?
Reply
#5
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
Reply
#6
[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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
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!
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing List values to get indexes Edward_ 7 1,082 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Struggling with Juggling JSON Data SamWatt 7 1,819 May-09-2022, 02:49 AM
Last Post: snippsat
  Syntax errors: Struggling to setup enviroment and load packages AH56 5 2,724 Jun-30-2021, 01:01 PM
Last Post: AH56
  Struggling for the past hour to define function and call it back godlyredwall 2 2,156 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  struggling with != statements CallumRoberts2004 2 1,509 Aug-18-2020, 03:01 PM
Last Post: GOTO10
  How to print array indexes? Mark17 6 2,686 Aug-03-2020, 04:26 PM
Last Post: Mark17
  Struggling with nested list gr3yali3n 3 2,257 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Understand list comprehension and draw PUP280 9 3,888 Apr-23-2020, 05:25 PM
Last Post: PUP280
  Struggling to exit this while loop fatherted99 5 2,412 Feb-08-2020, 07:46 PM
Last Post: fatherted99

Forum Jump:

User Panel Messages

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