Newbie Here, need clarification please

Hello, I am trying to teach myself python. I am needing a little clarification on this:

a = ['a', 'b', 'c']
 >>> n = [1, 2, 3]
 >>> x = [a, n] 
>>> x
 [['a', 'b', 'c'], [1, 2, 3]]
 >>> x[0] 
['a', 'b', 'c'] 
>>> x[0][1]
 'b'

I understand up until the >>> x[0] results being [‘a’, ‘b’, ‘c’]
I even understand the next line …[1] being ‘b’
Maybe it is so obvious that I am over complicating it. IDK
Does the zero mean all characters after the zero position?
Thanks for any help!

No. Zero means ‘the first index position only’. If you want ‘all “items” after the zero position’, you use [1:], as in:

>>> x = [0,1,2,4,5,6]
>>> x[1:]
[1, 2, 4, 5, 6]

This x[0][1] means ‘the second position of the first list’, given that you have a nested list.

Im confused, if the zero means the first index position why does it return [‘a’, ‘b’, ‘c’]? Why not just [‘a’]?

Because (as I say), you have a ‘nested list’: that is to say you have two list object, within another list object. So, [0] is returning ‘the first list object’.

x = [a, n] has created a list object that contains the other two list objects.

ahhh! I get it now.
a[0] would be ‘a’
n[0] would be '1;
and x[0] is nested list ‘a’ so its ‘a’,‘b’,‘c’

any suggestions on books or websites to help me learn?
Right now im using python.org tutorial.

thanks

Way-to-go! You’ve got it.

A couple of sites that I’d recommend:

Because (as I say), you have a ‘nested list’: that is to say you have
two list object, within another list object. So, [0] is returning
‘the first list object’.

x = [a, n] has created a list object that contains the other two list
objects.

To make this a bit more clear maybe, your first post showed this:

 >>> x
 [['a', 'b', 'c'], [1, 2, 3]]

Laid out more elaboratedly:

 [
   ['a', 'b', 'c'],  # this is x[0]
   [1, 2, 3]         # this is x[1]
 ]

so for example x[1][1] is [1,2,3][1] which is 2.

Cheers,
Cameron Simpson cs@cskk.id.au

Okay, thank you.

Thanks everyone!

1 Like