[Tutor] <mutable>.__mul__

Alan Gauld alan.gauld at btinternet.com
Sat Aug 3 18:41:01 CEST 2013


On 03/08/13 15:50, Albert-Jan Roskam wrote:

> Suppose I initialize  a list (letÅ› say it's a record) to e.g all zeroes,
 > or all sixes. Suppose, further, that I use "*" for this
> (which is a nice an clean way).

Its only nice if you use it at the top level with an immutable value, 
otherwise , as you can see, it quickly becomes not nice and not clean.

Use a list comprehension instead

y = [[6] for i in range(4)]

As the name suggests list comprehensions are designed for building lists.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list