convert a int to a list

Gary Wessle phddas at yahoo.com
Fri Apr 28 16:59:01 EDT 2006


Hi

can type conversion work to convert an int to a list?
I am trying to solve an problem in one tutorial.


****************************************************************
a = ['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]

    As an exercise, write a loop that traverses the previous list and
    prints the length of each element. What happens if you send an
    integer to len? 
****************************************************************

for i in a:
    print len(a[i])

will not do.
the list has str, int, list, list.
I am expecting the output to be 1, 1, 3, 3 which are the number of
elements of each element of a, someone might think the result should
be 4, 3, 3 which is len(a), len(a[2]), len(a[3]) but how can I do both
thoughts with a loop?


thank you



More information about the Python-list mailing list