Passing Dictionary as Arguments to Function in Python
Last Updated :
12 Jul, 2025
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)
Outputkey: 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)
Outputkey: 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'])
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)
Outputkey: 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.
Related Articles
Similar Reads
Passing Dictionary as Arguments to Function - Python 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 mo
4 min read
Passing function as an argument in Python In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, decorators and lambda expressions. By passing a function as an argument, we can modify a functionâs behavior dynamically
5 min read
Python | Passing dictionary as keyword arguments Many times while working with Python dictionaries, due to advent of OOP Paradigm, Modularity is focussed in different facets of programming. Hence there can be many use cases in which we require to pass a dictionary as argument to a function. But this required the unpacking of dictionary keys as arg
3 min read
How to Add Function in Python Dictionary Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks
4 min read
Tuple as function arguments in Python Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different c
2 min read
Pass function and arguments from node.js to Python Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine lear
4 min read
Passing Dictionary as Arguments to Function in Python
min read