A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/packing-and-unpacking-arguments-in-python/ below:

Packing and Unpacking Arguments in Python

Packing and Unpacking Arguments in Python

Last Updated : 18 Feb, 2025

Python provides the concept of packing and unpacking arguments, which allows us to handle variable-length arguments efficiently. This feature is useful when we don’t know beforehand how many arguments will be passed to a function.

Packing Arguments

Packing allows multiple values to be combined into a single parameter using * (for tuples/lists) and ** (for dictionaries).

1. Packing with *args

The * operator allows us to pass multiple arguments to a function and pack them into a tuple.

Example Code: Python
def sample(*args):
    print("Packed arguments:", args)

sample(1, 2, 3, 4, "geeks for geeks")

Output
Packed arguments: (1, 2, 3, 4, 'geeks for geeks')

Explanation:

2. Packing with **kwargs

** operator is used to collect multiple keyword arguments into a dictionary.

Code Python
def sample(**kwargs):
    print("Packed keyword arguments:", kwargs)

sample(name="Anaya", age=25, country="India")

Output
Packed keyword arguments: {'name': 'Anaya', 'age': 25, 'country': 'India'}

Explanation:

Unpacking Arguments

Unpacking allows values from an iterable (list, tuple, or dictionary) to be passed as separate arguments to a function.

1. Unpacking a List/Tuple with *

We use * to unpack elements from a list/tuple.

Example Python
def addition(a, b, c):
    return a + b + c

num = (1, 5, 10)  
result = addition(*num) 
print("Sum:", result)

Explanation: *numbers unpacks numbers into a, b, c.

2. Unpacking a Dictionary with **

We use ** to unpack key-value pairs from a dictionary.

Example

Python
def info(name, age, country):
    print(f"Name: {name}, Age: {age}, Country: {country}")

data = {"name": "geeks for geeks", "age": 30, "country": "India"}
info(**data)  

Output
Name: geeks for geeks, Age: 30, Country: India

Explanation: **data unpack dictionary values and assign them to parameters.

Packing and Unpacking Together

We can use Packing and Unpacking in same function.

Example Python
def together(*args, **kwargs):
    print("Positional:", args)
    print("Keyword arguments:", kwargs)

together(1, 2, 3, name="geeks for geeks", age=30)

Output
Positional: (1, 2, 3)
Keyword arguments: {'name': 'geeks for geeks', 'age': 30}

Explanation:

Difference between Packing and Unpacking

Features

Packing

Unpacking

Definition

Collects multiple values into a single variable (tuple, list, or dictionary).

Extracts values from a collection (tuple, list, or dictionary) into individual variables.

Operator Used

* (for tuples/lists), ** (for dictionaries).

* (for lists/tuples), ** (for dictionaries).

Purpose

Allows functions to accept a variable number of arguments.

Allows passing values dynamically to functions or variables.

Data Structure

Combine multiple values into a single entity (tuple or dictionary).

Extracts multiple values from a single entity into separate variables.

Example in function definition

def func(*args): (packs multiple positional arguments into a tuple)
def func(**kwargs): (packs multiple keyword arguments into a dictionary)

func(*list_var) (unpacks elements from a list/tuple)
func(**dict_var) (unpacks key-value pairs from a dictionary)

Example inf unction call

func(1, 2, 3, 4) → Packs (1, 2, 3, 4) into args.

func(*[1, 2, 3, 4]) → Unpacks [1, 2, 3, 4] as separate arguments.

Storage Format

Tuple (*args) or Dictionary (**kwargs).

Individual variables or function arguments.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4