Dictionaries of Lists

Discussion in 'Python' started by gf gf, Mar 8, 2005.

  1. gf gf

    gf gf Guest

    I'd like to associate certain lists with keywords, and
    retrieve them. But this is not possible as lists are
    not hashable.

    What is the best workaround? I don't mind making my
    lists immutable. Is there a way to tupelize them?
    I tried mydict[mykey]=([a for a in list]) but it
    didn't seem to work.

    Thanks!

    PS If possible please cc me on all responses.





    __________________________________
    Celebrate Yahoo!'s 10th Birthday!
    Yahoo! Netrospective: 100 Moments of the Web
    http://birthday.yahoo.com/netrospective/
    gf gf, Mar 8, 2005
    #1
    1. Advertising

  2. gf gf wrote:

    > I'd like to associate certain lists with keywords, and
    > retrieve them. But this is not possible as lists are
    > not hashable.


    You can convert them to tuples with the `tuple' function:

    aDictionary[tuple(aList)] = aKeyword

    > What is the best workaround? I don't mind making my
    > lists immutable. Is there a way to tupelize them?
    > I tried mydict[mykey]=([a for a in list]) but it
    > didn't seem to work.


    This code sample seems to suggest you're trying to do something else,
    however. If `mykey' is a string, then what you wrote is completely
    valid regardless of whether you converted it to a list or not.

    If that was just a thinko while you were asking the question, then the
    reason ([...]) didn't work is that it's just syntactically wrapping a
    list comprehension in parentheses, rather than turning them into a tuple.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Dead men have no victory.
    -- Euripides
    Erik Max Francis, Mar 8, 2005
    #2
    1. Advertising

  3. gf gf

    Robert Kern Guest

    gf gf wrote:
    > I'd like to associate certain lists with keywords, and
    > retrieve them. But this is not possible as lists are
    > not hashable.


    Do you want

    mydict[mylist] = mykey

    or

    mydict[mykey] = mylist

    ?

    The former is not possible with lists for the reason you noted. The
    latter, however, works just fine. It is only the key that needs to be
    hashable. The value can be any object.

    > What is the best workaround? I don't mind making my
    > lists immutable. Is there a way to tupelize them?


    tuple(mylist)

    > I tried mydict[mykey]=([a for a in list]) but it
    > didn't seem to work.


    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Mar 8, 2005
    #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. Narendra C. Tulpule

    Are lists at least as efficient as dictionaries?

    Narendra C. Tulpule, Aug 29, 2003, in forum: Python
    Replies:
    6
    Views:
    308
    Chad Netzer
    Aug 30, 2003
  2. Alex Martelli

    dictionaries with nested lists

    Alex Martelli, Nov 1, 2003, in forum: Python
    Replies:
    6
    Views:
    366
    Francis Avila
    Nov 2, 2003
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    377
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. lysdexia
    Replies:
    6
    Views:
    466
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    466
    Brandon
    Aug 15, 2008
Loading...

Share This Page