Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict.fromkeys - Odd logic when passing second dict.fromkeys as value #45629

Closed
dohertywa mannequin opened this issue Oct 16, 2007 · 2 comments
Closed

dict.fromkeys - Odd logic when passing second dict.fromkeys as value #45629

dohertywa mannequin opened this issue Oct 16, 2007 · 2 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@dohertywa
Copy link
Mannequin

dohertywa mannequin commented Oct 16, 2007

BPO 1288
Nosy @rhettinger
Files
  • problem-report.txt
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2007-10-16.22:35:47.622>
    created_at = <Date 2007-10-16.20:27:31.241>
    labels = ['interpreter-core', 'type-bug', 'invalid']
    title = 'dict.fromkeys - Odd logic when passing second dict.fromkeys as value'
    updated_at = <Date 2007-10-16.22:35:47.615>
    user = 'https://bugs.python.org/dohertywa'

    bugs.python.org fields:

    activity = <Date 2007-10-16.22:35:47.615>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2007-10-16.22:35:47.622>
    closer = 'rhettinger'
    components = ['Interpreter Core']
    creation = <Date 2007-10-16.20:27:31.241>
    creator = 'dohertywa'
    dependencies = []
    files = ['8556']
    hgrepos = []
    issue_num = 1288
    keywords = []
    message_count = 2.0
    messages = ['56507', '56508']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'dohertywa']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1288'
    versions = ['Python 2.5', 'Python 2.4', 'Python 2.3']

    @dohertywa
    Copy link
    Mannequin Author

    dohertywa mannequin commented Oct 16, 2007

    Hello:

    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
    using:
    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.

    @dohertywa dohertywa mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 16, 2007
    @rhettinger
    Copy link
    Contributor

    This isn't a bug. Writing
    "dict.fromkeys(xrange(1,48),dict.fromkeys(xrange(1,8),0))" results in
    the inner expression being evaluated just once and then passed to the
    outer function call as a fully evaluated argument. As a result, the
    *same* dictionary is being used over and over again.

    People commonly encounter similar issue when they try to create
    initialized list-of-lists with something like s=[[0]10] which repeats
    ten of the *same
    lists. Instead they should write something like:
    s=[[0] for i in range(10)] which creates *distinct* inner lists.

    For you application, consider using a defaultdict which can call a
    function as needed to create new, distinct values:

      d = defaultdict(lambda: dict.fromkeys(xrange(1,8), 0))

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant