0

I would like to find the intersection of lists that are stored within the defaultdict(list) container. Here is my dictionary, 'd' a list of lookup values, 'my_list':

d = { a: ['1', '2', '3'],
      b: ['3', '4', '5'],
      c: ['3', '6', '7']
     }

my_list = ['a', 'b']

I would like to return the intersection of the lists. Based on a previous post I tried the following but get an error: TypeError: unhashable type: 'list'

set.intersection(*map(set,d[my_list]))

any suggestions would be welcome.

thanks, zach cp

Community
  • 1
  • 1
zach
  • 29,475
  • 16
  • 67
  • 88

1 Answers1

9

The problem is that you are trying to access d[my_list] – a list is not a vlaid dictionary key. One alternative:

set.intersection(*(set(d[k]) for k in my_list))
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841