Syntactic sugar for assignment statements: one value to multiple targets?

Roy Smith roy at
Thu Aug 18 15:13:14 CEST 2011

In article 
<16ea4848-db0c-489a-968c-ca40700f5806 at>,
 gc <gc1223 at> wrote:

> I frequently need to initialize several variables to the same
> value, as I'm sure many do. Sometimes the value is a constant, often
> zero; sometimes it's more particular, such as defaultdict(list). I use
> dict() below.

Keep in mind that when you do:

a = dict()
b = dict()

you are NOT initializing a and b to the same value.  You are 
initializing each of them to a different empty dictionary, which is very 
different from

a = b = dict()

I suspect you knew that, but it's worth mentioning.

> # Option 1 (separate lines)
> # Verbose and annoying, particularly when the varnames are long and of
> irregular length
> a = dict()
> b = dict()
> c = dict()
> d = dict()
> e = dict()

This seems the best to me.  Simple, straight-forward, easy to 
understand.  What could be bad?  It may not be elegant, but if I could 
have a nickel for every hour I've wasted trying to understand elegant 
code, I'd be a rich man.  I can understand the above code in an instant, 
even at 2 AM juiced up on sugar and caffeine.

More information about the Python-list mailing list