FAQ: Function Arguments: *args and **kwargs - Variable number of arguments: *args

This community-built FAQ covers the “Variable number of arguments: *args” exercise from the lesson “Function Arguments: *args and **kwargs”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Variable number of arguments: *args

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I didn’t know where to post this so I thought I’d try my luck here. I’m try to write a function to find the product of integer arguments…

def combinations(*args):
	num = 1
	for n in args:
		num *= n if n else 1
	return num

print(combinations(6, 7, 0))#, 42)

The code works great. However, below if my attempt at using list comprehension to solve the same problem but I am getting an error. Any assistance or pointers will be highly appreciated.

def combinations(*args):
	return [num *= n for n in args if n else 1]

print(combinations(6, 7, 0))#, 42)

A comprehension is a list object composed of expressions or expression evaluations. num *= n is not an expression, but a statement.

@mtf How then would i go about solving this? Assuming there is a way to do that

There is no practical way to do with a comprehension what your first example does… return a product.

Not sure one would give the name combinations to a function that returns a product of all its arguments. Again, the first example is a sensible approach, assuming zero is meant to be ignored.

@mtf Much appreciated

1 Like