[Tutor] Question about list
Hoffmann
oasf2004 at yahoo.com
Tue Apr 11 00:48:01 CEST 2006
--- Adam <adam.jtm30 at gmail.com> wrote:
> On 10/04/06, Hoffmann <oasf2004 at yahoo.com> 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
>
> instead of just print list1[i] you could use print
> list1[i], len(list1[i]).
> I'd like to point out that the usual way to iterate
> through objects in
> a list like that would be using a for loop like so.
>
> for item in list1:
> print item, len(item)
>
> as you can see this is much easier to understand and
> is also a lot shorter.
> HTH.
>
Hi Adam,
In the previous email, I forgot to mention that I have
already tried:
i = 0
while i < len(list1):
print list1[i], len(list1[i])
i += 1
However, I got:
soam! 5
1
Traceback (most recent call last):
File "list1.py", line 11, in ?
print list1[i], len(list1[i])
TypeError: len() of unsized object
Could I hear from you again?
Hoffmann
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
More information about the Tutor
mailing list