Open In App

Passing Dictionary as Arguments to Function in Python

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Passing a dictionary as an argument to a function in Python allows you to work with structured data in a more flexible and efficient manner. For example, given a dictionary d = {"name": "Alice", "age": 30}, you can pass it to a function and access its values in a structured way. Let's explore the most efficient methods to pass a dictionary as an argument to a function.

Passing Dictionary as an argument

This is the most straightforward and efficient approach where you pass the entire dictionary to the function and process it inside the function.

Python
def fun(d):
    for key in d:
        print("key:", key, "value:", d[key])
        
        
d1 = {'a': 1, 'b': 2, 'c': 3}
fun(d1)

Output
key: a value: 1
key: b value: 2
key: c value: 3

Explanation: fun(d) accepts a dictionary d, iterates through its keys, accesses each corresponding value with d[key] and prints the key-value pair in the format "key: <key> value: <value>".

Using ** to unpack the dictionary

If you know the dictionary has specific keys that match function parameters, you can unpack the dictionary and pass the key-value pairs as keyword arguments.

Python
def fun(**d):
    for key, value in d.items():
        print("key:", key, "Value:", value)

d1 = {'a': 1, 'b': 2, 'c': 3}
fun(**d1)

Output
key: a Value: 1
key: b Value: 2
key: c Value: 3

Explanation: fun(**d) accepts keyword arguments, iterates through the passed key-value pairs using d.items() and prints each key-value pair in the format "key: <key> Value: <value>".

Modifying Dictionary Values

Dictionaries are mutable, meaning their values can be directly modified within a function. When a dictionary is passed as an argument to a function, changes to its values will persist outside the function scope.

Python
def fun(p):
    p['age'] += 1

d = {'name': 'John Doe', 'age': 25}
fun(d)
print(d['age'])

Output
26

Explanation: fun(d) updates the 'age' in the dictionary d, changing it from 25 to 26. Since dictionaries can be changed directly, the update is reflected outside the function too.

Using **kwargs

This approach uses **kwargs to accept any number of keyword arguments and prints them. This is a flexible approach, especially if the dictionary may have varying keys.

Python
def fun(**kwargs):
    for key, value in kwargs.items():
        print("key:", key, "Value:", value)

d1 = {'a': 1, 'b': 2, 'c': 3}
fun(**d1)

Output
key: a Value: 1
key: b Value: 2
key: c Value: 3

Explanation: fun(**kwargs) accepts keyword arguments and iterates through each key-value pair using kwargs.items(). By unpacking d1 using **d1, the dictionary d1 = {'a': 1, 'b': 2, 'c': 3} is passed as individual key-value pairs.

When to Use Dictionary as Arguments?

Passing a dictionary to a function is especially useful when you have a group of related parameters that need to be processed together. Some common scenarios include:

  • Handling configurations where multiple settings (like database configurations, API configurations, etc.) are passed as a dictionary.
  • Working with complex data structures where the function might need to handle multiple data points of different types and a dictionary provides a structured way to pass this information.

Similar Reads