3

I came across a blog post with the following function call:

intersection_cardinality = len(set.intersection(*[x, y]))

Is there any benefit in passing parameters as an unpacked array created just for that, instead of simply calling set.intersection(x, y)?

The blog post was written in python2, but the question goes for 3 as well.

2
  • 1
    In that case, this is nothing but syntactic sugar. I'm not even sure it is so sweet. Commented Feb 16, 2018 at 13:10
  • 1
    set.intersection(*[x, y]) is an overcomplicated way of writing the simple expression x & y Commented Feb 16, 2018 at 13:33

1 Answer 1

2

In the example you provided, there is no real point in using this syntax.

There are cases though where creating an array or tuple to unpack it can be useful.

# Python3

def print_n_times(n, string):
    # Instead of doing a loop we unpack an array of length n
    print(*n*(string,), sep='\n')
Sign up to request clarification or add additional context in comments.

1 Comment

It's also useful to unpack generators and generator expressions to pass to a variadic function like print or zip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.