

{"id":7393,"date":"2018-02-07T10:06:08","date_gmt":"2018-02-07T04:36:08","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7393"},"modified":"2026-04-22T15:48:08","modified_gmt":"2026-04-22T10:18:08","slug":"python-defaultdict","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/","title":{"rendered":"Python Defaultdict &#8211; Int and List as a Default Factory"},"content":{"rendered":"<p>In today\u2019s lesson, we will look at Python defaultdict, a subclass of the built-in dict class. So let&#8217;s start with defaultdict Python 3 tutorial.<\/p>\n<p>Here, we will discuss what is Python Defaultdict with its syntax and exmaple. Moreover, we will study Python Defaultdict using Int and List as a defaultdict in Python.<\/p>\n<p>Dictionaries are an important part of Python. They are containers to hold key-value pairs.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; issubclass(defaultdict,dict)\u00a0\u00a0 #You\u2019ll need to import defaultdict from collections for this<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>So, let&#8217;s start Python Defualtdict Tutorial.<\/p>\n<h3>What are Python Dictionaries?<\/h3>\n<p>Before we proceed to defaultdict in Python, we suggest you read up on Python Dictionaries.<\/p>\n<p>A Python dictionary is a container that holds key-value pairs. Keys must be unique, immutable objects. This means that a Python tuple can be a key, but a Python list can\u2019t since it is mutable.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; {[1,2]:1,2:2}\r\nTraceback (most recent call last):\r\nFile \"&lt;pyshell#355&gt;\", line 1, in &lt;module&gt;\r\n{[1,2]:1,2:2}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">TypeError: unhashable type: &#8216;list&#8217;<\/div>\n<p>As you can see, a list, then, is unhashable. Let\u2019s take a quick example to see how to define a python dictionary.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; mydict={'a':1,'b':2,'c':3}\r\n&gt;&gt;&gt; mydict['b']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>We can also use the dict() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict=dict((['a',1],['b',2],['c',3]))\r\n&gt;&gt;&gt; otherdict<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>{&#8216;a&#8217;: 1, &#8216;b&#8217;: 2, &#8216;c&#8217;: 3}<\/p>\n<\/div>\n<p>To get a list of keys, we use the keys() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict.keys()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>dict_keys([&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;])<\/p>\n<\/div>\n<p>But we have one problem. If the key does not exist, and we try to access it, it raises a KeyError.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict['d']\r\nTraceback (most recent call last):\r\nFile \"&lt;pyshell#364&gt;\", line 1, in &lt;module&gt;\r\notherdict['d']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>KeyError: &#8216;d&#8217;<\/p>\n<\/div>\n<h3>What is Python Defaultdict?<\/h3>\n<p>To deal with this situation, we have defaultdict in Python. First, we must import it from the Python collections module.<\/p>\n<p><strong>Characteristics of Defaultdict in Python:<\/strong><\/p>\n<ul>\n<li><strong>Missing behaviour:<\/strong> It automatically provides a default value if a problem occurs.<\/li>\n<li><strong>Default value:<\/strong> It can create its own value like 0 or an empty list.<\/li>\n<li><strong>Code simplicity:<\/strong> It can make the code shorter and easier to read, when it is needed.<\/li>\n<li><strong>Flexibility:<\/strong> It is considered to be more flexible to do repetitive tasks than Dict.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import defaultdict<\/pre>\n<p>Here, we use the Python defaultdict() factory function. It takes, as an argument, a function object that will return a value.<\/p>\n<p>Actually, if we don\u2019t provide a value for a particular key, it will take that value for it.<\/p>\n<p>Let\u2019s take a look at the syntax.<\/p>\n<h3>Python Defaultdict &#8211; Syntax<\/h3>\n<p>Before Python defaultdict syntax, we revise python syntax.<\/p>\n<p>As we discussed, we first import defaultdict from the \u2018collections\u2019 module. Let us see this Python defaultdict example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import defaultdict<\/pre>\n<p>Next, we define a Python function to return a default value for the keys we don\u2019t initialize.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def defaultvalue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 0<\/pre>\n<p>Now, we create a Python defaultdict using the defaultdict() function. To this, we pass the function defaultvalue as a function argument.<\/p>\n<p>Remember, you must pass the function object; you don\u2019t have to call it with parentheses.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict=defaultdict(defaultvalue)<\/pre>\n<p>Let\u2019s now define values for keys \u2018a\u2019, \u2018b\u2019, and \u2018c\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict['a']=1\r\n&gt;&gt;&gt; otherdict['b']=2\r\n&gt;&gt;&gt; otherdict['c']=3<\/pre>\n<p>What if we access the value for key \u2018d\u2019? We haven\u2019t initialized it yet.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; otherdict['d']<\/pre>\n<p>As you can see, it assigned it the default value we specified through the function defaultvalue().<\/p>\n<p>Let\u2019s see the whole code at once now.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def defaultvalue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 0\r\n&gt;&gt;&gt; otherdict=defaultdict(defaultvalue)\r\n&gt;&gt;&gt; otherdict['a']=1\r\n&gt;&gt;&gt; otherdict['b']=2\r\n&gt;&gt;&gt; otherdict['c']=3\r\n&gt;&gt;&gt; otherdict['d']<\/pre>\n<h3>Another Example of Python Defaultdict<\/h3>\n<p>Let\u2019s take another Python defaultdict example. We take a defaultdict in Python to hold the marks of students. Also, we let the default value for marks be 35.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; marks=defaultdict(lambda :35)\r\n&gt;&gt;&gt; marks['Ayushi']=95\r\n&gt;&gt;&gt; marks['Gigi']=86\r\n&gt;&gt;&gt; marks['Ayushi']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">95<\/div>\n<p>Here, we used a Lambda Expression since all we need to do in it is to return a value. So why waste space, right? Now, if we try to access the value of a key that doesn\u2019t exist, it saves 35 to it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; marks['Oshin']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">35<\/div>\n<p>Finally, these are the keys to this Python defaultdict now:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; marks.keys()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>dict_keys([&#8216;Ayushi&#8217;, &#8216;Gigi&#8217;, &#8216;Oshin&#8217;])<\/p>\n<\/div>\n<h3>More on Defaultdict in Python<\/h3>\n<p>Let\u2019s see what happens if we return None from the function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=defaultdict(lambda :None)\r\n&gt;&gt;&gt; a[1]\r\n&gt;&gt;&gt; type(a[1])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&lt;class &#8216;NoneType&#8217;&gt;<\/p>\n<\/div>\n<p>What if a Python exception was raised in the factory function?<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; def defaultvalue():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(1\/0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return 0\r\n&gt;&gt;&gt; a=defaultdict(defaultvalue)\r\n&gt;&gt;&gt; a[1]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#386&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>a[1]<\/p>\n<p>File &#8220;&lt;pyshell#384&gt;&#8221;, line 2, in defaultvalue<\/p>\n<p>print(1\/0)<\/p>\n<p>ZeroDivisionError: division by zero<\/p>\n<\/div>\n<p>Python defaultdict supports all operations that a regular dictionary does. These include keys() and len().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; dir(a)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[&#8216;__class__&#8217;, &#8216;__contains__&#8217;, &#8216;__copy__&#8217;, &#8216;__delattr__&#8217;, &#8216;__delitem__&#8217;, &#8216;__dir__&#8217;, &#8216;__doc__&#8217;, &#8216;__eq__&#8217;, &#8216;__format__&#8217;, &#8216;__ge__&#8217;, &#8216;__getattribute__&#8217;, &#8216;__getitem__&#8217;, &#8216;__gt__&#8217;, &#8216;__hash__&#8217;, &#8216;__init__&#8217;, &#8216;__init_subclass__&#8217;, &#8216;__iter__&#8217;, &#8216;__le__&#8217;, &#8216;__len__&#8217;, &#8216;__lt__&#8217;, &#8216;__missing__&#8217;, &#8216;__ne__&#8217;, &#8216;__new__&#8217;, &#8216;__reduce__&#8217;, &#8216;__reduce_ex__&#8217;, &#8216;__repr__&#8217;, &#8216;__setattr__&#8217;, &#8216;__setitem__&#8217;, &#8216;__sizeof__&#8217;, &#8216;__str__&#8217;, &#8216;__subclasshook__&#8217;, &#8216;clear&#8217;, &#8216;copy&#8217;, &#8216;default_factory&#8217;, &#8216;fromkeys&#8217;, &#8216;get&#8217;, &#8216;items&#8217;, &#8216;keys&#8217;, &#8216;pop&#8217;, &#8216;popitem&#8217;, &#8216;setdefault&#8217;, &#8216;update&#8217;, &#8216;values&#8217;]<\/p>\n<\/div>\n<p>Let&#8217;s know about Exception handling in Python.<\/p>\n<h4>1. __missing__(key) in Python<\/h4>\n<p>This Python method is called by the __getitem__() method of the dict class when the requested key isn\u2019t found.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import defaultdict\r\n&gt;&gt;&gt; a=defaultdict(lambda :0)\r\n&gt;&gt;&gt; a['a']=1\r\n&gt;&gt;&gt; a['b']=2\r\n&gt;&gt;&gt; a['c']=3\r\n&gt;&gt;&gt; a.__missing__('d')<\/pre>\n<p>It returned 0 here because that\u2019s what we specified as a default value through a lambda expression to the defaultdict() factory function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a.__getitem__('a')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>1<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a.__getitem__('e')<\/pre>\n<h3>Using \u2018list\u2019 as a Default Factory<\/h3>\n<p>If we pass \u2018list\u2019 (without the quotes) to the defaultdict() Python function, we can group a sequence of key-value pairs into a dictionary of lists.<\/p>\n<p>We can do this for more types as well. We\u2019ll see another in the next section.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=[('a',1),('b',2),('c',3)]\r\n&gt;&gt;&gt; b=defaultdict(list)\r\n&gt;&gt;&gt; for i,j in a:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 b[i].append(j)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n&gt;&gt;&gt; b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>defaultdict(&lt;class &#8216;list&#8217;&gt;, {&#8216;a&#8217;: [1], &#8216;b&#8217;: [2], &#8216;c&#8217;: [3]})<\/p>\n<\/div>\n<p>Let\u2019s explain what this code does.<\/p>\n<p>First, we define \u2018a\u2019 as a list of tuples to hold the key-value pairs. Next, we pass \u2018list\u2019 to defaultdict(), and store this in \u2018b\u2019. This tells the interpreter that b will hold a dictionary with values that are list.<\/p>\n<p>Then, we traverse on the tuples using names \u2018I\u2019 and \u2018j\u2019 with a for-loop. In this, we append j to b[i].<\/p>\n<p>What this means is that we take the second value in each tuple in \u2018a\u2019, and append it to the python defaultdict for each key (first value in each tuple).<\/p>\n<p>For \u2018c\u2019 in the list \u2018a\u2019, as a key in the defaultdict \u2018b\u2019, we append the value 3 to the python defaultdict to that key. This is its value now.<\/p>\n<h3>Using \u2018int\u2019 as a Default Factory<\/h3>\n<p>We can tell the interpreter to create a Python dictionary of integers. Here\u2019s how.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; name='Bubbles'\r\n&gt;&gt;&gt; mydict=defaultdict(int)\r\n&gt;&gt;&gt; for i in name:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mydict[i]+=1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n&gt;&gt;&gt; mydict<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>defaultdict(&lt;class &#8216;int&#8217;&gt;, {&#8216;B&#8217;: 1, &#8216;u&#8217;: 1, &#8216;b&#8217;: 2, &#8216;l&#8217;: 1, &#8216;e&#8217;: 1, &#8216;s&#8217;: 1})<\/p>\n<\/div>\n<p>This way, we can use it as a counter. Of course, we could have done this with a regular dictionary as well. But here, we tell the interpreter that we\u2019re going to work with only integer values.<\/p>\n<p>So, this was all about Python Defaultdict Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Defaultdict<\/h3>\n<p>1. What is Defaultdict in Python?<\/p>\n<p>2. What is the difference between Defaultdict and dict?<\/p>\n<p>3. How does Defaultdict work in Python?<\/p>\n<p>4. Explain the use of Defaultdict in Python.<\/p>\n<p>5. Is Defaultdict slower than dict?<\/p>\n<h3>Conclusion<\/h3>\n<p>defaultdict fills missing keys with a default factory, such as list or int, saving repeated if key not in dict checks.<\/p>\n<p>With a subtle difference from a regular dictionary, we see that a Python defaultdict is actually quite useful. It has a simple syntax as well and lets us define a default value for uninitialized keys.<\/p>\n<p>It also lets us tell the interpreter that we want to work with a particular type of value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In today\u2019s lesson, we will look at Python defaultdict, a subclass of the built-in dict class. So let&#8217;s start with defaultdict Python 3 tutorial. Here, we will discuss what is Python Defaultdict with its&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":7426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[3717,4319,10485,10486,10487,14085],"class_list":["post-7393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-defaultdict-in-python","tag-example-of-python-defaultdict","tag-python-defaultdict","tag-python-defaultdict-example","tag-python-defaultdict-syntax","tag-syntax-of-python-defaultdict"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Defaultdict - Int and List as a Default Factory - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn What is Defaultdict in python with syntax and example, Using int &amp; list as a default factory in Defaultdict in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-defaultdict\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Defaultdict - Int and List as a Default Factory - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn What is Defaultdict in python with syntax and example, Using int &amp; list as a default factory in Defaultdict in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-defaultdict\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-07T04:36:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-22T10:18:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Defaultdict - Int and List as a Default Factory - DataFlair","description":"Learn What is Defaultdict in python with syntax and example, Using int & list as a default factory in Defaultdict in Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/","og_locale":"en_US","og_type":"article","og_title":"Python Defaultdict - Int and List as a Default Factory - DataFlair","og_description":"Learn What is Defaultdict in python with syntax and example, Using int & list as a default factory in Defaultdict in Python","og_url":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-07T04:36:08+00:00","article_modified_time":"2026-04-22T10:18:08+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Defaultdict &#8211; Int and List as a Default Factory","datePublished":"2018-02-07T04:36:08+00:00","dateModified":"2026-04-22T10:18:08+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/"},"wordCount":1142,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg","keywords":["defaultdict in python","Example of Python Defaultdict","Python defaultdict","Python Defaultdict Example","Python Defaultdict Syntax","Syntax of Python Defaultdict"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-defaultdict\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/","url":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/","name":"Python Defaultdict - Int and List as a Default Factory - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg","datePublished":"2018-02-07T04:36:08+00:00","dateModified":"2026-04-22T10:18:08+00:00","description":"Learn What is Defaultdict in python with syntax and example, Using int & list as a default factory in Defaultdict in Python","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-defaultdict\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-defaultdict-01.jpg","width":1200,"height":628,"caption":"What is Python Defaultdict"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-defaultdict\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Defaultdict &#8211; Int and List as a Default Factory"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7393","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=7393"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7393\/revisions"}],"predecessor-version":[{"id":147779,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7393\/revisions\/147779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/7426"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}