RE: Dictionary .keys() and .values() should return a set[withPython3000 in mind]

Discussion in 'Python' started by Delaney, Timothy (Tim), Jul 3, 2006.

  1. Paul Rubin wrote:

    > "Delaney, Timothy (Tim)" <> writes:
    >> The eventual consensus was that keys(), etc should return views on
    >> the dictionary. These views would be re-iterable and will have
    >> basically the same behaviour as the lists returned from keys(), etc.
    >> However, such a view could have O(1) __contains__ behaviour, and
    >> would not incur the overhead of creating a list and populating it -
    >> they would be backed by the dictionary. Of course, this introduces
    >> the issue of concurrent modification, but you can always get an
    >> independent copy by calling list(dict.keys()).

    >
    > Wait a sec, you're saying
    >
    > k0 = d.keys()
    > d['whee'] = 'parrot' # this can change k0???
    >
    > That sounds broken.


    That's the problem with views. You either take a snapshot of the current
    state, or you deal with the backing store changing. Java's solution is
    to raise a ConcurrentModificationException when it determines this has
    happened (with lots of weasel words about no guarantees).

    If you want an independent data set, you have to take a snapshot. For
    the above, that's doing:

    k0 = list(d.keys())

    or

    k0 = set(d.keys())

    depending on what behaviour you want.

    Tim Delaney
     
    Delaney, Timothy (Tim), Jul 3, 2006
    #1
    1. Advertising

  2. Delaney, Timothy (Tim)

    Paul Rubin Guest

    Re: Dictionary .keys() and .values() should return a set [withPython3000 in mind]

    "Delaney, Timothy (Tim)" <> writes:
    > If you want an independent data set, you have to take a snapshot. For
    > the above, that's doing:
    >
    > k0 = list(d.keys())


    I don't understand. Why have .keys() at all, if it doesn't get you
    an independent data set? If all you want is to iterate through the
    dict, you can already do that:

    for k in d: ....
     
    Paul Rubin, Jul 3, 2006
    #2
    1. Advertising

  3. Delaney, Timothy (Tim)

    Guest

    Re: Dictionary .keys() and .values() should return a set [withPython3000 in mind]

    You bring up a good point. the "for x in d:" notation is a relativly
    new construction, "for x in d.keys()" is much older. Some of the value
    of d.keys() goes away because we have this new construction, but
    there's some reasons to keep it around:
    1. Consitency. You can get the values, you can get the (key, value)
    pairs. it'd be odd not to be able to get the keys
    2. Choices. if d.keys() is a FAST operation, then you can then use
    that to create any structure you want. For example, if you want a set,
    set(d.keys()) only requires you to create a set. If d.keys() created
    an independant list, python would first need to create a list, then
    create a set.
    Paul Rubin wrote:
    > "Delaney, Timothy (Tim)" <> writes:
    > > If you want an independent data set, you have to take a snapshot. For
    > > the above, that's doing:
    > >
    > > k0 = list(d.keys())

    >
    > I don't understand. Why have .keys() at all, if it doesn't get you
    > an independent data set? If all you want is to iterate through the
    > dict, you can already do that:
    >
    > for k in d: ....
     
    , Jul 3, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christoph Zwerschke

    Why is dictionary.keys() a list and not a set?

    Christoph Zwerschke, Nov 23, 2005, in forum: Python
    Replies:
    90
    Views:
    1,652
    Mike Meyer
    Nov 29, 2005
  2. Girish Sahani
    Replies:
    11
    Views:
    619
    Roberto Bonvallet
    Jun 7, 2006
  3. Replies:
    14
    Views:
    589
    Antoon Pardon
    Jul 4, 2006
  4. Delaney, Timothy (Tim)
    Replies:
    4
    Views:
    328
  5. ++imanshu
    Replies:
    7
    Views:
    261
Loading...

Share This Page