[Tutor] Question about list
adam.jtm30 at gmail.com
Tue Apr 11 00:42:54 CEST 2006
On 10/04/06, Hoffmann <oasf2004 at yahoo.com> wrote:
> 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:
> ['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?
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.
More information about the Tutor