# 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 arguments and it’s values as argument values. Let’s discuss a method in which this can be performed.

Method : Using ** ( splat ) operator This operator is used to unpack a dictionary, and while passing in a function this can possibly unpack a dictionary and achieve the required task of mapping the keys to arguments and it’s values to argument values.

## Python3

 `# Python3 code to demonstrate working of` `# Passing dictionary as keyword arguments` `# Using ** ( splat ) operator`   `# Helper function to demo this task` `def` `test_func(a ``=` `4``, b ``=` `5``):` `    ``print``("The value of a ``is` `: " ``+` `str``(a))` `    ``print``("The value of b ``is` `: " ``+` `str``(b))`   `# initializing dictionary` `test_dict ``=` `{``'a'` `: ``1``, ``'b'` `: ``2``}`   `# printing original dictionary` `print``("The original dictionary ``is` `: " ``+` `str``(test_dict))`   `# Testing with default values ` `print``("The default function call yields : ")` `test_func()`   `print``("\r")`   `# Passing dictionary as keyword arguments` `# Using ** ( splat ) operator` `print``("The function values with splat operator unpacking : ")` `test_func(``*``*``test_dict)`

Time Complexity: O(1)

Space Complexity: O(n) -> n = number of key-value pairs

Output :

```The original dictionary is : {'a': 1, 'b': 2}
The default function call yields :
The value of a is : 4
The value of b is : 5

The function values with splat operator unpacking :
The value of a is : 1
The value of b is : 2```

Method 2 : Single argument to the function and then unpack it inside the function

step-by-step approach

Define a function test_func that takes a single argument arg_dict.
Inside the function, use the get() method of dictionaries to retrieve the values of a and b from the arg_dict dictionary. If the keys a or b are not present in the dictionary, use default values of 4 and 5, respectively.
Print the values of a and b.
Define a dictionary test_dict with keys a and b and corresponding values.
Print the original dictionary test_dict.
Call the function test_func with an empty dictionary as an argument to test with default values.
Print a newline character for better readability.
Call the function test_func with the test_dict dictionary as an argument and unpack the dictionary inside the function.

## Python3

 `# Python3 code to demonstrate working of` `# Passing dictionary as single argument and unpacking inside function`   `# Helper function to demo this task` `def` `test_func(arg_dict):` `    ``a ``=` `arg_dict.get(``'a'``, ``4``)` `    ``b ``=` `arg_dict.get(``'b'``, ``5``)` `    ``print``(``"The value of a is: "` `+` `str``(a))` `    ``print``(``"The value of b is: "` `+` `str``(b))`   `# initializing dictionary` `test_dict ``=` `{``'a'``: ``1``, ``'b'``: ``2``}`   `# printing original dictionary` `print``(``"The original dictionary is: "` `+` `str``(test_dict))`   `# Testing with default values` `print``(``"The default function call yields:"``)` `test_func({})`   `print``(``"\r"``)`   `# Passing dictionary as single argument and unpacking inside function` `print``(``"The function values with dictionary unpacking:"``)` `test_func(test_dict)`

Output

```The original dictionary is: {'a': 1, 'b': 2}
The default function call yields:
The value of a is: 4
The value of b is: 5

The function values with dictionary unpacking:
The value of a is: 1
The value of b is: 2```

Time complexity: O(1) as the dictionary is unpacked inside the function and does not require any additional time.

Auxiliary space: O(1) as we are not creating any new data structures, but only retrieving values from the existing dictionary.
{"ft_lang_label":"__label__en","ft_lang_prob":0.61335355,"math_prob":0.9072425,"size":3116,"snap":"2023-40-2023-50","text_gpt3_token_len":790,"char_repetition_ratio":0.20147815,"word_repetition_ratio":0.13181019,"special_character_ratio":0.27342746,"punctuation_ratio":0.1266892,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9893946,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-05T02:55:51Z\",\"WARC-Record-ID\":\"<urn:uuid:5c89423c-bdb5-4923-bbb1-d099e003ae9c>\",\"Content-Length\":\"307314\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:979e0a16-300a-41cc-8cff-eac7225fa6a8>\",\"WARC-Concurrent-To\":\"<urn:uuid:8a48ec71-0fc7-453a-8e97-19d133ef0462>\",\"WARC-IP-Address\":\"23.45.181.178\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-passing-dictionary-as-keyword-arguments/\",\"WARC-Payload-Digest\":\"sha1:B3GJU65WN2HMLWDOYJ2W5S6B45CF4XKF\",\"WARC-Block-Digest\":\"sha1:KXWXF5OS27JDWVFH4OLBFGEJXNKJ2R7F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511717.69_warc_CC-MAIN-20231005012006-20231005042006-00331.warc.gz\"}"}