
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Private Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Walrus Operator
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python - Floating Points
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Packing and Unpacking
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Namedtuple
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Multiple Inheritance
- Python - Multilevel Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python - Data Classes
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Warnings
- Python - Built-in Exceptions
- Python - Debugger (PDB)
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Generator Expressions
- Python - Lambda Expressions
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - Odds and Ends
- Python - GUIs
- Python Advanced Concepts
- Python - Abstract Base Classes
- Python - Custom Exceptions
- Python - Higher Order Functions
- Python - Object Internals
- Python - Memory Management
- Python - Metaclasses
- Python - Metaprogramming with Metaclasses
- Python - Mocking and Stubbing
- Python - Monkey Patching
- Python - Signal Handling
- Python - Type Hints
- Python - Automation Tutorial
- Python - Humanize Package
- Python - Context Managers
- Python - Coroutines
- Python - Descriptors
- Python - Diagnosing and Fixing Memory Leaks
- Python - Immutable Data Structures
- Python Useful Resources
- Python - Questions & Answers
- Python - Interview Questions & Answers
- Python - Online Quiz
- Python - Quick Guide
- Python - Reference
- Python - Cheatsheet
- Python - Projects
- Python - Useful Resources
- Python - Discussion
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
Packing and Unpacking in Python
Packing and unpacking are a techniques used in Python to handle multiple values in a single variable. This is similar to how we pack items into a bag and unpack them when needed. Python functions can be used to accept a packed set of arguments, which can then be unpacked inside the function for further processing. This is called as packing and unpacking of arguments.
In this chapter, we will discuss all the details of packing and unpacking in Python and how it is performed inside function arguments. Following are the topics that we will cover in this chapter −
- What is Packing and Unpacking?
- Packing and Unpacking Tuples in Python
- Packing Arguments in Python
- Unpacking Arguments in Python
- Use Case of Packing and Unpacking Arguments
- Comparison of Packing and Unpacking Arguments
What is Packing and Unpacking?
Packing is a technique used to group multiple values into a single variable. For example, while calling a function, you can pass multiple arguments by packing them into a tuple or list. Unpacking is just the reverse process of packing. It is used to extract individual values from a packed variable and assign them to separate variables. Generally, an unpacking is performed inside any functions to access the individual values from a packed variable.
In Python, packing and unpacking can be done using the following symbols −
- * − This is used to pack or unpack a variable number of positional arguments.
- ** − This is used to pack or unpack a variable number of keyword arguments.
Let's look at an example to understand how packing and unpacking works in Python −
# Packing Example def pack_example(*args): print("Packed arguments:", args) pack_example(1, 2, 3, 'a', 'b') # Unpacking Example def unpack_example(a, b, c): print("Unpacked arguments:", a, b, c) values = [1, 2, 3] unpack_example(*values)
The output of the above code will be −
Packed arguments: (1, 2, 3, 'a', 'b') Unpacked arguments: 1 2 3
Packing and Unpacking Tuples in Python
In the context of tuples, packing refers to the process of creating a tuple by grouping multiple values together. Similarly, unpacking refers to the process of extracting individual values from a tuple and assigning them to separate variables. This operation is useful for functions that return multiple values as a tuple.
You can use following syntax to pack and unpack tuples in Python −
# Packing a tuple my_tuple = (1, 2, 3) # Unpacking a tuple a, b, c = my_tuple
Example 1
In this example, the tuple contains 6 values and variables to be unpacked are 3. We prefix "*" to the second variable.
tup1 = (10,20,30, 40, 50, 60) x, *y, z = tup1 print ("x: ",x, "y: ", y, "z: ", z)
It will produce the following output −
x: 10 y: [20, 30, 40, 50] z: 60
Here, the values are unpacked in "x" and "z" first, and then the rest of the values are assigned to "y" as a list.
Example 2
In this example, we will add the prefix "*" to the first variable and see what will be the output.
tup1 = (10,20,30, 40, 50, 60) *x, y, z = tup1 print ("x: ",x, "y: ", y, "z: ", z)
It will produce the following output −
x: [10, 20, 30, 40] y: 50 z: 60
Here again, the tuple is unpacked in such a way that individual variables take up a single value first, leaving the remaining values to the list "x".
Packing Arguments in Python
The packing arguments refer to the process of collecting multiple arguments into a single function parameter. This is useful when you pass an iterable (like a list or tuple) or a dictionary to a function and want to combine them into a single parameter.
There are two ways to pack arguments in Python -
- *args: This is used to pack a multiple number of positional arguments into a tuple.
- **kwargs**: This is used to pack a multiple number of keyword arguments into a dictionary.
Packing Arguments with *args
In the following example, we declared a function that uses *args to combine multiple positional arguments into a single tuple. We then print the whole tuple using a single print statement.
def pack_args(*args): print("Packed arguments:", args) # Calling the function with multiple arguments pack_args(1, 2, 3, 'a', 'b')
The output of the above code will be −
Packed arguments: (1, 2, 3, 'a', 'b')
Packing Arguments with **kwargs
In the example below, we declared a function that uses **kwargs to combine multiple keyword arguments into a single dictionary. The function then prints the whole dictionary.
def pack_kwargs(**kwargs): print("Packed keyword arguments:", kwargs) # Calling the function with multiple keyword arguments pack_kwargs(name="Alice", age=30, city="New York")
The output of the above code will be −
Packed keyword arguments: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Unpacking Arguments in Python
The unpacking arguments is just the opposite of packing arguments. In this case, we will pass an entire collection (like a list, tuple, or dictionary) to a function and unpack its elements into individual arguments inside the function. There are two ways to unpack arguments in Python.
Unpacking Arguments with *
The * operator is used to unpack a list or tuple into individual positional arguments. In the example shown below, we declared a function that takes three positional arguments. We then unpack a list of values into individual arguments when calling the function.
def unpack_args(a, b, c): print("Unpacked arguments:", a, b, c) # List of values to unpack values = [1, 2, 3] # Calling the function with unpacked values unpack_args(*values)
The output of the above code will be −
Unpacked arguments: 1 2 3
Unpacking Arguments with **
The ** operator is used to unpack a dictionary into individual keyword arguments. In the following example, we declared a function that takes three keyword arguments. We then unpack a dictionary of values into individual keyword arguments when calling the function.
def unpack_kwargs(name, age, city): print("Unpacked keyword arguments:", name, age, city) # Dictionary of values to unpack info = {"name": "Farhan", "age": 25, "city": "Los Angeles"} # Calling the function with unpacked values unpack_kwargs(**info)
The output of the above code will be −
Unpacked keyword arguments: Farhan 25 Los Angeles
Real-World Use-Case of Packing and Unpacking Arguments
Packing and unpacking arguments are commonly used in real-world applications. Here is an example of a shopping cart application where we use packing and unpacking arguments to handle variable numbers of items and their details.
In this example, we have two functions: one for calculating the total price of items in the cart using packing with *args, and one for printing the invoice details using packing with **kwargs. We also show unpacking a list of item prices and a dictionary of item details when calling these functions.
# Function that accepts variable number of items (packing with *args) def calculate_total(*items): print("Items in cart:", items) return sum(items) # Function that accepts variable keyword arguments (packing with **kwargs) def print_invoice(customer_name, **details): print(f"Invoice for {customer_name}") for key, value in details.items(): print(f"{key}: {value}") # _ Unpacking Example _ # Order items (list of prices) order_items = [250, 100, 75] # Prices of products total_amount = calculate_total(*order_items) # Unpacking list print("Total Amount:", total_amount) # Order details (dictionary) order_details = { "Product": "Laptop", "Quantity": 1, "Price": total_amount, "Payment Method": "Credit Card" } # Passing dictionary as keyword args print_invoice("Farhan", **order_details)
The output of the above code will be −
Items in cart: (250, 100, 75) Total Amount: 425 Invoice for Farhan Product: Laptop Quantity: 1 Price: 425 Payment Method: Credit Card
Comparison of Packing and Unpacking Arguments
Here is a detailed comparison of packing and unpacking arguments in Python −
Feature | Packing Arguments | Unpacking Arguments |
---|---|---|
Definition | Packing arguments is the process of collecting multiple arguments into a single parameter using *args or **kwargs. | Unpacking arguments is the process of breaking down a collection (like a list, tuple, or dictionary) into individual arguments using * or **. |
Purpose | Allows functions to accept a variable number of arguments. | Allows passing values dynamically to functions or variables. |
Working | Combine multiple values into a single entity (tuple or dictionary). | Break down a single entity (list, tuple, or dictionary) into multiple values. |
Use Case | Commonly used when the number of arguments is unknown or variable. | Commonly used when you have a collection of values that need to be passed as individual arguments. |
Example Syntax | def func(*args, **kwargs): | func(*list_of_values) or func(**dict_of_values) |
Conclusion
Packing and unpacking arguments are new features in Python that allow us to write flexible function definitions and calls. One important thing to note is that when using both *args and **kwargs in a function definition, *args must appear before **kwargs. This ensures that positional arguments are collected first, then the keyword arguments.