0

I am trying to understand why my intersection does not return an empty list when I run this code.

n = ([1,2,3,4,5],[3,4,5,6],[5,6,7],[7,8,9,10,11,12],[10,22,33,44,45])
w = set(n[0]).intersection(*n[:1])
print(w)
#Returns (1,2,3,4,5)

However this returns the correct set

n = ([1,2,3,4,5],[3,4,5,6],[5,6,7],[7,8,9,10,11,12],[10,22,33,44,45])
w = set(n[0]).intersection(*n)
print(w)
#Returns empty set.

This question gave the correct results for both:
Python -Intersection of multiple lists?

Why do I not get the correct result when I compare the first set to the remaining lists?

Community
  • 1
  • 1
Dennis Simpson
  • 85
  • 1
  • 10

1 Answers1

7

*n[:1] unpacks to (n[0],) - the first element of the set.

So you're intersecting n[0] with itself, and the result is what you see. You probably intended to write

set(n[0]).intersection(*n[1:])
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561