retreving data from a sublist

Peter Hansen peter at engcorp.com
Tue May 6 09:07:32 EDT 2003


Michael Fortune wrote:
> 
> how do you retreive data from a sublist
> 
> i.e. list[[45,89,00,],[8,37,22]]
> 
> i want to print only 37
> tryied print list[2[1]]

This is Python: try the interactive prompt and teach yourself!

Python 2.2 (#1, Apr 12 2002, 15:29:57)
[GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
>>> x = [[45, 89, 00, ], [8, 37, 22]]
>>> x
[[45, 89, 0], [8, 37, 22]]
>>> x[2[1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
>>> x[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list index out of range
>>> x[1]
[8, 37, 22]
>>> x[1[1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
>>> 1[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsubscriptable object
>>> x[1][1]
37
>>> Yay!!!!
  File "<string>", line 1
    Yay!!!!
       ^
SyntaxError: invalid syntax




More information about the Python-list mailing list