aerain
aerain

Reputation: 591

A list as a key for a dictionary

I have multiple lists of tuples eg

[([1, 2, 3, 4], 2), ([5, 6, 7], 3)]

that I would like to have as keys to a dictionary (so each key in my dictionary would be a list of tuples).

Unfortunately, according to the TypeError I'm getting (unhashable type: list), it seems that python doesn't like hashing lists. All of the elements in my list of tuples are ints (if that makes an difference). Any suggestions on what I can do? Thanks!

Upvotes: 2

Views: 367

Answers (5)

rubik
rubik

Reputation: 9104

Convert your lists to tuples:

dict((tuple(a), b) for a,b in [([1,2,3,4],2),([5,6,7],3)])

If you are using Python >= 2.7 you can use dict-comprehensions:

{tuple(a): b for a,b in [([1,2,3,4],2),([5,6,7],3)]}

Upvotes: 2

eyquem
eyquem

Reputation: 27575

Use repr

class A:
    pass

import time

# A and time as heterogenous elements, only to show the generality of my solution

li_li = [ [([1,2,3,4],2),([5,6,7],3)] ,
          [([10,20,3],2),      ([5,6,77],3)] ,
          [([1,2,3,4],2),([5,6,time,7],3),([875,12], ['jk',78], A, (100,23),'zuum')] ]




didi = {}
for i,li in enumerate(li_li):
    didi[repr(li)] = i

print 'dictionary  didi:'
for k,v in didi.iteritems():
    print k,'     ',v

print '----------------------------------'

print didi[repr([([1+1+1+1+1+5,         200000/10000,    3],2),([5,8-2,7*11],3)      ])]

result

dictionary  didi:
[([1, 2, 3, 4], 2), ([5, 6, <module 'time' (built-in)>, 7], 3), ([875, 12], ['jk', 78], <class __main__.A at 0x011CFC70>, (100, 23), 'zuum')]       2
[([1, 2, 3, 4], 2), ([5, 6, 7], 3)]       0
[([10, 20, 3], 2), ([5, 6, 77], 3)]       1
----------------------------------
1

Upvotes: 1

JAB
JAB

Reputation: 21079

>>> def nested_lists_to_tuples(ls):
    return tuple(nested_lists_to_tuples(l) if isinstance(l, (list, tuple)) else l for l in ls)

>>> nested_lists_to_tuples([([1,2,3,4],2),([5,6,7],3)])
(((1, 2, 3, 4), 2), ((5, 6, 7), 3))

Then just use that returned value as your key. Note that I did it this way so you could support even more deeply nested mixes of tuples and lists, like [([1,(2, [3, 4, [5, 6, (7, 8)]]), 3, 4], 2), ([5, 6, 7], 3)]:

>>> nested_lists_to_tuples([([1, (2, [3, 4, [5, 6, (7, 8)]]), 3, 4], 2), ([5, 6, 7], 3)])
(((1, (2, (3, 4, (5, 6, (7, 8)))), 3, 4), 2), ((5, 6, 7), 3))

There may possibly be a simpler way to do this, though.

Upvotes: 2

RiaD
RiaD

Reputation: 47619

You should convert lists to tuples

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798576

Use tuples instead.

>>> dict((tuple(x[0]), x[1]) for x in [([1,2,3,4],2),([5,6,7],3)])
{(5, 6, 7): 3, (1, 2, 3, 4): 2}

Upvotes: 4

Related Questions