list example

Edward Elliott nobody at 127.0.0.1
Sat Apr 22 16:35:24 EDT 2006


No substantive problems.  The str() calls are unnecessary, print calls 
list.__str__ already.  You can replace the loop with list comprehensions or 
slices.  Same result, a bit more succinct.  See these pages for more:

http://docs.python.org/lib/typesseq.html
http://docs.python.org/tut/node7.html  (section 5.1.4)


PAolo wrote:
> Hi,
> I wrote this small example to illustrate the usage of lists:
> 
> even=[]
> odd=[]
> 
> for i in range(1,10):
>         if i%2:
>                 odd.append(i)
>         else:
>                 even.append(i)
> 
> print "odd: "+str(odd)
> print "even: "+str(even)
> 
> numbers=even
> numbers.extend(odd)
> print "numbers:"+str(numbers)
> numbers.sort()
> print "sorted numbers:"+str(numbers)
> 
> 
> any comment, suggestion? Is there something not elegant?
> 
> Thnx
> PAolo
> 



More information about the Python-list mailing list