0

This works as expected because I know the length of the list. I know that there are 3 items. I need 0, 1 and 2 as shown below:

xdict={'x':[{'a', 'b', 'c'}, {'a', 'b', 'c', 'd'}, {'a', 'b'}]}
xdict['x'][0] & xdict['x'][1] & xdict['x'][2]

But when I do not know the count of list, how to I get the same results? I tried something like this...

for i in range(len(xdict['x'])):
    print (xdict['x'][i])
shantanuo
  • 31,689
  • 78
  • 245
  • 403
  • 2
    Does this answer your question? [Best way to find the intersection of multiple sets?](https://stackoverflow.com/questions/2541752/best-way-to-find-the-intersection-of-multiple-sets) – Tomerikoo May 06 '20 at 16:37
  • blhsing's answer is very good. Just an alternative solution. `functools.reduce(lambda x,y: x&y,xdict['x'])` – Ch3steR May 06 '20 at 16:38

1 Answers1

4

set.intersection takes multiple set arguments, so you can simply unpack the list of sets as arguments to set.intersection:

set.intersection(*xdict['x'])
blhsing
  • 91,368
  • 6
  • 71
  • 106