Re: lists and tuples

Discussion in 'Python' started by Fredrik Lundh, Jun 28, 2003.

  1. Gerrit Holl wrote:

    > This is probably a basic question, but I can't see why mutable
    > objects can't be used as dictionairy keys. Why would this be
    > impossible:
    >
    > 0 >>> d = {}
    > 1 >>> l = []
    > 2 >>> d[l] = "foo"
    > 3 >>> d
    > {[]: 'foo'}
    > 4 >>> l.append("bar")
    > 5 >>> l
    > ['bar']
    > 6 >>> d
    > {['bar']: 'foo'}
    >
    > What is the problem of doing this?


    it's explained in the Python language reference:

    The only types of values not acceptable as keys are values
    containing lists or dictionaries or other mutable types that
    are compared by value rather than by object identity, the
    reason being that the efficient implementation of dictionaries
    requires a key's hash value to remain constant.

    (anyone thinking of following up with "but use a tree" or "make all
    objects observable" etc should check the archives first (or write
    their own Python-compatible-but-slower language, and compete
    in the marketplace...))

    </F>
     
    Fredrik Lundh, Jun 28, 2003
    #1
    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. Daniel Nogradi
    Replies:
    3
    Views:
    351
    Dennis Lee Bieber
    Nov 10, 2006
  2. Replies:
    5
    Views:
    560
    Thomas J. Gritzan
    Oct 6, 2006
  3. tuples within tuples

    , Oct 26, 2007, in forum: Python
    Replies:
    12
    Views:
    584
    Dennis Lee Bieber
    Oct 27, 2007
  4. xera121
    Replies:
    8
    Views:
    729
    lolmc
    Sep 30, 2009
  5. Jon Reyes
    Replies:
    18
    Views:
    240
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page