Back to all tutorials
pythonfunctionsargskwargspython-functions-series

*args and **kwargs in Python Explained Like a Potluck Party

Learn *args and **kwargs in Python with a simple potluck party analogy. Understand variable-length arguments without the confusion.

The Spark Engineer 2026-08-25 5 min read

*args and **kwargs in Python, Explained Like a Potluck Party

Imagine you're hosting a potluck. You have no idea how many dishes people will bring—two, maybe twenty. You can't design a form with a fixed number of "dish" fields. You just need to include as many dishes as come. That's exactly the problem *args and **kwargs solve in Python.

The Problem They Solve

A normal function needs a fixed number of parameters.

PYTHON
def bring_dishes(dish1, dish2): print(dish1, dish2)
  • This works if you only have two dishes: e.g. bring_dishes("pasta", "salad")
  • But what if someone brings three dishes: e.g. bring_dishes("pasta", "salad", "pie")
  • Python will give you an error because the function only has two parameters.
  • That's where *args comes in handy. You can pass any number of arguments.

*args: Any Number of Unnamed Items

*args allows a function to accept any number of positional arguments.

Python
def bring_dishes(*args):    for dish in args:        print(f"{dish} has arrived at the potluck") bring_dishes("pasta", "salad", "pie")

Here, args becomes ("pasta", "salad", "pie")

The name args is just a convention; what really matters is the asterisk * preceding it.

**kwargs: Any Number of Labeled Items

Now imagine each dish also needs a label with who brought it. That's a name-value pair, exactly what **kwargs is built for. It collects any number of keyword arguments into a dictionary.

Python
def bring_dishes(**kwargs):    for person, dish in kwargs.items():        print(f"{person} brought {dish}") bring_dishes(alex="pasta", sam="salad", jordan="pie")

Here, kwargs becomes {"alex": "pasta", "sam": "salad", "jordan": "pie"}. Again, kwargs is just a naming convention, the double asterisk ** is what makes it work.

.items() is a dictionary method that lets you loop through both keys and values at the same time.

Using Both Together

A potluck has a host and may also have many dishes and guests. In the same way, a Python function can use regular parameters along with *args and **kwargs to handle extra information.

PYTHON
def potluck(host, *args, **kwargs): print(f"Potluck hosted by {host}") for dish in args: print(f"Unlabeled dish: {dish}") for person, dish in kwargs.items(): print(f"{person} brought {dish}") potluck("Maria", "chips", "soda", alex="pasta", sam="salad")
> Output Potluck hosted by Maria Unlabeled dish: chips Unlabeled dish: soda alex brought pasta sam brought salad

The order matters here: regular parameters come first, then *args, then **kwargs.

Unpacking: Using * and ** When Calling a Function

The same symbols work in reverse. If you already have a list or dictionary, you can "unpack" it straight into a function call instead of typing each item manually.

PYTHON
dishes = ["pasta", "salad", "pie"] bring_dishes(*dishes) # same as bring_dishes("pasta", "salad", "pie") guests = {"alex": "pasta", "sam": "salad"} bring_dishes(**guests) # same as bring_dishes(alex="pasta", sam="salad")

When You'll Actually Use This

You'll rarely design a potluck sign-up sheet in real life, but you'll constantly write functions that wrap other functions, create flexible utilities, or accept configuration options you can't anticipate. *args and **kwargs are similar to how popular libraries like Flask and Django let you pass arbitrary settings to their functions.

What's Next

Now that your functions can handle any number of guests, the next tutorial covers lambda functions, which are quick functions to create when writing a complete def block seems excessive.

...
Share

Discussion & Thoughts

Join the conversation with your Google Account

0 Comments

Sign in to leave a comment

We use 1-click Google authentication to prevent bot spam and verify authentic developer discussions.

Loading discussions...