[New-bugs-announce] [issue1288] dict.fromkeys - Odd logic when passing second dict.fromkeys as value

Adam Doherty report at bugs.python.org
Tue Oct 16 22:27:31 CEST 2007

New submission from Adam Doherty:


I'm am trying to conduct some tests on a list of data that checks for
the position of values in list elements using the bisect module.  To
store the results of these tests for output to a template I have build a
dictionary with 47 keys the values of which are dictionaries themselves.
 These inner dictionaries contain 7 keys that initially are valued at
zero.  Looping through the data in my list I check for values from 1 to
47 and if I find the value I am looking for I lookup it's position in
the row using the bisect module.  Using the current value I am looking
for and the position returned from bisect I increase the value in the
matching dictionary key value position by 1. Now for speed I have built
the dictionary using d =
dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0)) as this gives
the desired result.  Unfortunately when I run my test for values each
value in the dictionary is listed as the total number of rows in the
data list.  This is not the desired result which is correctly achieved
d = {}
for x in xrange(1,48):
    d[x] = dict.fromkeys(xrange(1,8),0)

I have included output from IDLE to demonstrate the problem.

components: Interpreter Core
files: problem-report.txt
messages: 56507
nosy: dohertywa
severity: normal
status: open
title: dict.fromkeys - Odd logic when passing second dict.fromkeys as value
type: behavior
versions: Python 2.3, Python 2.4, Python 2.5

Tracker <report at bugs.python.org>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: problem-report.txt
Url: http://mail.python.org/pipermail/new-bugs-announce/attachments/20071016/bdd5f55a/attachment.txt 

More information about the New-bugs-announce mailing list