Hi Hoffman,

It is often useful to use the "for" construct to process items in a list.
e.g.:

>>> list1 =  [ 'spam!', 2, ['Ted', 'Rock']]
>>> for item in list:
...    print item
spam!
2
['Ted', 'Rock']

If you pass a list to the len() function, it will return the number of
elenents in the list. e.g.:

>>> x = ['a', 'b', 'c']
>>> len(x)
3

Now if you pass len() a string it will return the length of a string:
>>> y = 'hello'
>>> len(y)
5

Given your list below, len() will return what you're looking for when it
encounters the third element of the list, but won't for the first and
second elements.  One way to solve this problem is to use the type()
function to figure out if your item is a string or list and use len()
as appropriate.  I hope this provides enough of a hint.

-mtw


On Mon, Apr 10, 2006 at 03:29:23PM -0700, Hoffmann ([EMAIL PROTECTED]) wrote:
> Hello,
> 
> I have a list: list1 =  [ 'spam!', 2, ['Ted', 'Rock']
> ]
> and I wrote the script below:
> 
> i = 0
> while i < len(list1):
>     print list1[i]
>     i += 1
> 
> Ok. This script will generate as the output each
> element of the original list, one per line:
> 
> spam!
> 2
> ['Ted', 'Rock']
> 
> I also would like to print the length of each element
> of that list:
> 
> spam! = 1 element
> 2 = 1 element
> ['Ted', 'Rock'] = 2 elements
> 
> Could anyone, please, give me some hints?
> Thanks,
> Hoffmann
>     
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to