[Tutor] (no subject) (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Jan 19 19:14:01 CET 2007


[Forwarding to tutor.  Can someone answer Max?  Slightly busy at the 
moment.  Quick note: get him to realize that the list he was playing with 
originally had three elements.  The example in his book has two elements. 
Something has to change.  *grin*]


---------- Forwarded message ----------
Date: Fri, 19 Jan 2007 10:10:04 -0800 (PST)
From: Max Jameson <max.jameson at sbcglobal.net>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] (no subject)

Thanks, Danny.

I expected to get the third element of the second list printed, as shown on the on-line tutorial I am reading: http://www.freenetpages.co.uk/hp/alan.gauld/

This is exactly how the tutorial reads (I omited the definition of the aList and anoterh):
>>> aList.append(another)
>>> print aList
[42, [1, 2, 7]]

Notice how the result is a list of two elements but the second element is itself a list (as shown by the []¢s around it). We can now access the element 7 by using a double index: 
>>> print aList[1][2]
7

The first index, 1, extracts the second element which is in turn a list. The second index, 2, extracts the third element of the sublist.


----- Original Message ----
From: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
To: Max Jameson <max.jameson at sbcglobal.net>
Cc: tutor at python.org
Sent: Friday, January 19, 2007 11:41:53 AM
Subject: Re: [Tutor] (no subject)


On Fri, 19 Jan 2007, Max Jameson wrote:

> I know this is totally off-the-wall, but what am I doing wrong here?  I 
> am using Python 2.5, and I am just learning...please be kind!


Hi Max,

Let's take a look.


###########################################
>>> aList = [2,3]
>>> bList = [5,7,9]
>>> aList.append(bList)
>>> print aList
[2, 3, [5, 7, 9]]
###########################################

Ok, looks good so far.  You have a list of three elements:

      2
      3
      [5, 7, 9]

where the third element in the list is itself a list.



###########################################
>>> print aList [1] [2]
Traceback (most recent call last):
    File "<pyshell#40>", line 1, in <module>
      print aList [1] [2]
TypeError: 'int' object is unsubscriptable
###########################################

Can you explain what you're trying to do at this point?  Pretend that we 
are clueless for the moment.

What are you expecting to get out from this?  We know you don't want to 
get the error, of course.  But what do you want to get?


Also, have you had a chance to look at something like:

     http://swaroopch.info/text/Byte_of_Python:Data_Structures


More information about the Tutor mailing list