{"id":1184,"date":"2019-12-18T09:32:26","date_gmt":"2019-12-18T09:32:26","guid":{"rendered":"https:\/\/www.askpython.com\/?p=1184"},"modified":"2019-12-18T09:32:28","modified_gmt":"2019-12-18T09:32:28","slug":"args-kwargs-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/args-kwargs-in-python","title":{"rendered":"*args and **kwargs in Python"},"content":{"rendered":"\n<p>Python provides some handy ways through which we could make a function take a variable number of arguments. <code>*args<\/code> and <code>**kwargs<\/code> do just that.<\/p>\n\n\n\n<p><code>*args<\/code> -&gt; Represents a <strong><em>List<\/em><\/strong> \/ <strong><em>Tuple<\/em><\/strong> of <strong>positional<\/strong> arguments to be passed to any function<\/p>\n\n\n\n<p><code>**kwargs<\/code> -&gt; Represents a <strong><em>Dictionary<\/em><\/strong> of <strong>keyword<\/strong> arguments to be passed to any function<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Purpose of *args<\/h2>\n\n\n\n<p><code>*args<\/code> is commonly used when you&#8217;re not sure about the number of arguments that you want to pass as function parameters, when you define the function. So essentially, this type of syntax allows us to pass an arbitrary number of arguments to the function, the exact number being determined at runtime. <\/p>\n\n\n\n<p>There are two cases where the <code>*<\/code> (star) operator differs in meaning.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Case 1 : In a function definition<\/h3>\n\n\n\n<p>Here, the <code>*<\/code>  operator is used to pack the arguments into a tuple\/list (which contains all the positional) arguments passed to the function. Hence, we use <code>*args<\/code> in the definition to signity that all the positional arguments passed to the function are packed into a list\/tuple called <code>args<\/code> (Any other name can be given, but it is common practice to write <code>*args<\/code> to signify that argument packing is used)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef find_average(*args):\n    total = 0\n    print(&#039;Packed Argument Tuple -&gt;&#039;, args)\n    for i in args:\n        total += i\n    return total \/ len(args)\n\n\nprint(&#039;Average -&gt;&#039;, find_average(1, 2, 3, 4, 5))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nPacked Argument Tuple -&gt; (1, 2, 3, 4, 5)\nAverage -&gt; 3.0\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Case 2: In a function call<\/h3>\n\n\n\n<p>Here, the <code>*<\/code> operator is used to unpack the corresponding list\/tuple passed to it, or even a generator.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;1, 2, 3]\nprint(*a)\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n1 2 3\n<\/pre><\/div>\n\n\n<p>This could be useful if you want an iterable to be expanded only the corresponding function is called.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Combining Case1 and Case2 to use *args<\/h3>\n\n\n\n<p>Here is an example using both <strong>Case1<\/strong> and <strong>Case2<\/strong> to compute the maximum of a list being unpacked and passed into a function, that takes a variable number of arguments.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef compute_maximum(*args):\n    maximum = 0\n    for i in args:\n        if i &gt; maximum:\n           maximum = i\n    return maximum\n\na = &#x5B;4, 5, 10, 14, 3]\nprint(&#039;Maximum -&gt;&#039;, compute_maximum(*a))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nMaximum -&gt; 14\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Purpose of **kwargs<\/h2>\n\n\n\n<p>Here, the <code>**<\/code> operator is used in a way similar to the previous situation, but this is applied exclusively to pack keyword arguments passed to a function into a Dictionary. The <code>**kwargs<\/code> idiom only applies to a function definition, and unlike <code>*args<\/code>, does not have any special meaning in a function call.<\/p>\n\n\n\n<p>Here is an example to illustrate the purpose of <code>**kwargs<\/code><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef find_average(**kwargs):\n    total = 0\n    print(&#039;Keyword Argument Dictionary -&gt;&#039;, kwargs)\n    for key, value in kwargs.items():\n        total += value\n    return total \/ len(kwargs.items())\n\n\nprint(&#039;Average -&gt;&#039;, find_average(first=1, second=2, third=3, fourth=4, fifth=5))\n<\/pre><\/div>\n\n\n<p>Output<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nKeyword Argument Dictionary -&gt; {&#039;first&#039;: 1, &#039;second&#039;: 2, &#039;third&#039;: 3, &#039;fourth&#039;: 4, &#039;fifth&#039;: 5}\nAverage -&gt; 3.0\n<\/pre><\/div>\n\n\n<p>The <code>*<\/code> operator can be used here to unpack the <code>**kwargs<\/code> and get all the <code>keys<\/code>\/<code>values<\/code> passed to the keyword Dictionary<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; print(*kwargs)\nfirst second third fourth fifth\n&gt;&gt;&gt; print(*kwargs.values())\n1 2 3 4 5\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This article helped us gain a deeper understanding of how <code>*args<\/code> and <code>**kwargs<\/code> can be used in function definitions to get a variable number of positional \/ keyword arguments and manipulate them, and how programmers use it in common practice for writing easy to use functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p>StackOverflow : <a href=\"https:\/\/stackoverflow.com\/questions\/3394835\/use-of-args-and-kwargs\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/3394835\/use-of-args-and-kwargs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python provides some handy ways through which we could make a function take a variable number of arguments. *args and **kwargs do just that. *args -&gt; Represents a List \/ Tuple of positional arguments to be passed to any function **kwargs -&gt; Represents a Dictionary of keyword arguments to be passed to any function Purpose [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1184","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=1184"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1184\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=1184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=1184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=1184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}