using zip() and dictionaries

Scott David Daniels Scott.Daniels at Acm.Org
Thu Apr 30 20:36:00 CEST 2009


Sneaky Wombat wrote:
> quick update,
> 
> #change this line:
> for (k,v) in zip(header,[[]]*len(header)):
> #to this line:
> for (k,v) in zip(header,[[],[],[],[]]):
> 
> and it works as expected.  Something about the [[]]*len(header) is
> causing the weird behavior.  I'm probably using it wrong, but if
> anyone can explain why that would happen, i'd appreciate it.

I'll bet the output below explains it:
     header = 'a', 'b', 'c', 'd'
     print [id(x) for x in [[]] * len(header)]
     print [id(x) for x in [[],[],[],[]]]
and (what I think you want):
     print [id(x) for x in [[] for x in header]]

So, I think you should use:
   columnMap = dict(zip(header, ([] for x in header)))

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list