Beginners question.

Jeremy Bowers jerf at jerf.org
Tue Aug 31 19:49:30 CEST 2004


On Tue, 31 Aug 2004 18:23:23 +0100, Player wrote:

> mylist = [1, 2, ["three", "four"], "five"]
> for item in mylist:
>     if item == type(list):
>         myitem = len(item)
>         print myitem
>     else:
>          print item

General tip: Don't forget how much fun the interpreter is:

Python 2.3.4 (#1, Jun  8 2004, 17:41:43) 
[GCC 3.3.3 20040217 (Gentoo Linux 3.3.3, propolice-3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> item = ["three", "four"] # since this is the one we wonder about
>>> item == type(list)
False
>>> # Uh-oh, there's the problem, let's see if we can get a clue:
... 
>>> item
['three', 'four']
>>> type(list)
<type 'type'>
>>> # Clearly, there's something wrong here; try Russell's solution:
... 
>>> type(item)
<type 'list'>
>>> list
<type 'list'>
>>> # Ah ha!
... 
>>> 




More information about the Python-list mailing list