

{"id":7721,"date":"2018-02-10T06:59:04","date_gmt":"2018-02-10T01:29:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7721"},"modified":"2026-04-24T15:20:02","modified_gmt":"2026-04-24T09:50:02","slug":"python-collections-module","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-collections-module\/","title":{"rendered":"Python Collections Module &#8211; Python Counter, DefaultDict, OrderedDict, NamedTuple"},"content":{"rendered":"<p>Over the last few days, we opened ourselves up to three subclasses of the class \u2018dict\u2019, and the function namedtuple().<\/p>\n<p>In this Python Collections Module tutorial, we will study Python Counter, Python DefaultDict, Python OrderedDict, and Python NamedTuple with their subtypes, syntax, and examples.<\/p>\n<p>So, let&#8217;s start Python Collection Modules.<\/p>\n<div id=\"attachment_42238\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-42238\" class=\"size-full wp-image-42238\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg\" alt=\"Python Collections Module - Introduction\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-42238\" class=\"wp-caption-text\">Python Collections Module &#8211; Introduction<\/p><\/div>\n<h3>What is the Python Collections Module?<\/h3>\n<p>The \u2018collections\u2019 module in Python that implements special container datatypes. These provide alternatives to Python\u2019s general-purpose built-in<span style=\"font-family: Verdana, Geneva, sans-serif;font-size: 16px;font-weight: inherit\"> containers.<\/span><\/p>\n<p>As we said, all three of these are subclasses of the Python class \u2018dict\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import Counter,defaultdict,OrderedDict,namedtuple\r\n&gt;&gt;&gt; issubclass(Counter,dict) and issubclass(defaultdict,dict) and issubclass(OrderedDict,dict)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; type(namedtuple)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;function&#8217;&gt;<\/div>\n<p>To use any of its functionality, we must first import it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import collections<\/pre>\n<div id=\"attachment_7735\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7735\" class=\"wp-image-7735 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01.jpg\" alt=\"Python Collections Module\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Collections-Module-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-7735\" class=\"wp-caption-text\">Python Collections Module<\/p><\/div>\n<p>Let&#8217;s see all the collections of Python<\/p>\n<h3>Python Collections Counter<\/h3>\n<p>Python counter is a container that keeps count of the number of occurrences of any value in the container. It counts hashable objects.<\/p>\n<p><strong>Features of the collection counter in Python:<\/strong><\/p>\n<ul>\n<li><strong>Missing Keys:<\/strong> It is not like a standard dictionary that raises a keyerror; counter returns 0 for any missing elements.<\/li>\n<li><strong>Negative count:<\/strong> The count can be of any value, anything from zero to negative.<\/li>\n<li><strong>Initialization:<\/strong> You can initialize the count from a list or string, a dictionary, or a keyword argument.<\/li>\n<\/ul>\n<p>Let\u2019s take an example.<\/p>\n<h4>1. Python Counter &#8211; Syntax<\/h4>\n<p>To define a Python counter, we use the Counter() factory function. To it, we can pass a container like Python list or a tuple, or even a string or a dictionary. We may also use keyword arguments.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import Counter\r\n&gt;&gt;&gt; c=Counter({'a':3,'b':2,'c':1})\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;a&#8217;: 3, &#8216;b&#8217;: 2, &#8216;c&#8217;: 1})<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter('Hello')\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;l&#8217;: 2, &#8216;H&#8217;: 1, &#8216;e&#8217;: 1, &#8216;o&#8217;: 1})<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter(a=3,b=2,c=1)\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;a&#8217;: 3, &#8216;b&#8217;: 2, &#8216;c&#8217;: 1})<\/div>\n<h4>2. Updating a Python Counter<\/h4>\n<p>To declare an empty counter in Python and then populate it, we use the update() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter()\r\n&gt;&gt;&gt; c.update('bfg')\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;b&#8217;: 1, &#8216;f&#8217;: 1, &#8216;g&#8217;: 1})<\/div>\n<h4>3. Accessing Counts in Python<\/h4>\n<p>To get the count of a value, we pass it as an index to the counter we defined.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c['f']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c['h']<\/pre>\n<p>As you can see, it does not raise a KeyError.<\/p>\n<p>What are Python Errors and How to Handle Python Errors?<\/p>\n<p>The elements() method returns a Python iterator for the values in the container.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in c.elements():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(f\"{i}: {c[i]}\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>b: 1f: 1<\/p>\n<p>g: 1<\/p>\n<\/div>\n<p>We can also call the most_common() method to get the n most common values. These are the ones with the highest frequencies.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter('hello')\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;l&#8217;: 2, &#8216;h&#8217;: 1, &#8216;e&#8217;: 1, &#8216;o&#8217;: 1})<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c.most_common(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[(&#8216;l&#8217;, 2), (&#8216;h&#8217;, 1)]<\/div>\n<h4>4. Python Counter Arithmetic<\/h4>\n<p>We can also perform arithmetic on Python counters.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c1=Counter('hello')\r\n&gt;&gt;&gt; c2=Counter('help')\r\n&gt;&gt;&gt; c1+c2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;l&#8217;: 3, &#8216;h&#8217;: 2, &#8216;e&#8217;: 2, &#8216;o&#8217;: 1, &#8216;p&#8217;: 1})<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c1&amp;c2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;h&#8217;: 1, &#8216;e&#8217;: 1, &#8216;l&#8217;: 1})<\/div>\n<h3>Python DefaultDict<\/h3>\n<p>Python DefaultDict collection lets us provide a default value for keys. We define it using the defaultdict() factory function, which takes another function as an argument. This function returns a default value for it.<\/p>\n<h4>1. Python DefaultDict &#8211; Syntax<\/h4>\n<p>To define\u00a0Python defaultdict, we use the factory function defaultdict().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import defaultdict\r\n&gt;&gt;&gt; d=defaultdict(lambda :35)\r\n&gt;&gt;&gt; d['Ayushi']=95\r\n&gt;&gt;&gt; d['Bree']=89\r\n&gt;&gt;&gt; d['Leo']=90.5\r\n&gt;&gt;&gt; d['Adam']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">35<\/div>\n<p>Here, we did not initialize \u2018Adam\u2019. So, it took 35, because that\u2019s what our function returns to defaultdict(). We can also check the default value with the __missing__() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d.__missing__('Adam')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">35<\/div>\n<h4>2. Using a Type as a Default Factory<\/h4>\n<p>We can tell the interpreter what type of values we\u2019re going to work with. We do this by passing it as an argument to defaultdict().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d=defaultdict(list)\r\n&gt;&gt;&gt; for i,j in [('a',(1,2)),('b',(3,4)),('c',(5,6))]:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 d[i].append(j)\r\n&gt;&gt;&gt; d<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">defaultdict(&lt;class &#8216;list&#8217;&gt;, {&#8216;a&#8217;: [(1, 2)], &#8216;b&#8217;: [(3, 4)], &#8216;c&#8217;: [(5, 6)]})<\/div>\n<h3>Python OrderedDict<\/h3>\n<p>Python OrderedDict remembers the order in which the key-value pairs were added. Let\u2019s take the Python ordereddict example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import OrderedDict\r\n&gt;&gt;&gt; o=OrderedDict()\r\n&gt;&gt;&gt; o['a']=3\r\n&gt;&gt;&gt; o['c']=1\r\n&gt;&gt;&gt; o['b']=4\r\n&gt;&gt;&gt; o<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">OrderedDict([(&#8216;a&#8217;, 3), (&#8216;c&#8217;, 1), (&#8216;b&#8217;, 4)])<\/div>\n<h4>1. Move_to_end() methods in Python<\/h4>\n<p>We\u2019ll take a look at two methods in Python, orderedDict. The first we discuss is move_to_end(). It lets us move a key-value pair either to the end or to the front.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; o.move_to_end('c')\r\n&gt;&gt;&gt; o<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">OrderedDict([(&#8216;a&#8217;, 3), (&#8216;b&#8217;, 4), (&#8216;c&#8217;, 1)])<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; o.move_to_end('c',last=False)\r\n&gt;&gt;&gt; o<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">OrderedDict([(&#8216;c&#8217;, 1), (&#8216;a&#8217;, 3), (&#8216;b&#8217;, 4)])<\/div>\n<h4>2. Popitem() methods in Python<\/h4>\n<p>This method lets us pop a key-value pair out of the container, and then displays it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; o.popitem()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;b&#8217;, 4)<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; o.popitem(last=False)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;c&#8217;, 1)<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; o<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">OrderedDict([(&#8216;a&#8217;, 3)])<\/div>\n<h3>Python NamedTuple<\/h3>\n<p>Finally, in the Python collections module we discuss Python NamedTuple. This is a container that lets us access elements using names\/labels.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import namedtuple\r\n&gt;&gt;&gt; colors=namedtuple('colors','r g b')\r\n&gt;&gt;&gt; red=colors(r=255,g=0,b=0)<\/pre>\n<h4>1. Accessing Elements<\/h4>\n<p>To access these elements, we use the dot Python operator.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red.r<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">255<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red.g<\/pre>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red.b<\/pre>\n<p>Or, we could just use indices.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red[0]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">255<\/div>\n<p>We can also use the getattr() function.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; getattr(red,'r')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">255<\/div>\n<p>Python namedtuple is immutable. So, you can\u2019t reassign a value directly.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red.r=3\r\nTraceback (most recent call last):\r\nFile \"&lt;pyshell#95&gt;\", line 1, in &lt;module&gt;\r\nred.r=3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">AttributeError: can&#8217;t set attribute<\/div>\n<h4>2. Converting into a Python Dictionary<\/h4>\n<p>To convert a namedtuple into a Python dictionary, we use the _asdict() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red._asdict()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">OrderedDict([(&#8216;r&#8217;, 255), (&#8216;g&#8217;, 0), (&#8216;b&#8217;, 0)])<\/div>\n<h4>3. Converting an Iterable into a namedtuple<\/h4>\n<p>The _make() method lets us create a namedtuple from a Python list and the format we specified.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; colors._make(['1','2','3'])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">colors(r=&#8217;1&#8242;, g=&#8217;2&#8242;, b=&#8217;3&#8242;)<\/div>\n<h4>4. Creating a namedtuple from the dictionary<\/h4>\n<p>To use a dictionary to make a Python namedtuple, we use this code:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; colors(**{'r':255,'g':0,'b':0})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">colors(r=255, g=0, b=0)<\/div>\n<h4>5. Checking What Fields Belong to the Tuple<\/h4>\n<p>For this, we have the _fields attribute.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red._fields<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(&#8216;r&#8217;, &#8216;g&#8217;, &#8216;b&#8217;)<\/div>\n<h4>6. Changing a Value<\/h4>\n<p>Like we said, Python namedtuples are immutable. But to change a value, we can use the _replace() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; red._replace(g=3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">colors(r=255, g=3, b=0)<\/div>\n<p>So, this was all about Python&#8217;s Collections Module Tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Collection Modules<\/h3>\n<ol>\n<li>What is the collection module in Python?<\/li>\n<li>What are collection types in Python?<\/li>\n<li>How do you use a collection counter in Python?<\/li>\n<li>What are the collection data types in Python?<\/li>\n<li>Is a string a collection in Python?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p>Now that we\u2019ve discussed all four Python collections modules- Counter, defaultdict, OrderedDict, and namedtuple with their syntax, methods, and examples of Python collections modules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the last few days, we opened ourselves up to three subclasses of the class \u2018dict\u2019, and the function namedtuple(). In this Python Collections Module tutorial, we will study Python Counter, Python DefaultDict, Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":42238,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2637,10428,10429,10431,10485,10709,10710,10747],"class_list":["post-7721","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-collections-module-in-python","tag-python-collection-modules","tag-python-collections","tag-python-collections-tutorial","tag-python-defaultdict","tag-python-namedtuple","tag-python-namedtuple-accessing-elements","tag-python-ordereddict"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Collections Module - Python Counter, DefaultDict, OrderedDict, NamedTuple - DataFlair<\/title>\n<meta name=\"description\" content=\"Python collections module - Collections in Python, Collections Counter, Python defaultdict, Python ordereddict, Python namedtuple with syntax &amp; examples\" \/>\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-collections-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Collections Module - Python Counter, DefaultDict, OrderedDict, NamedTuple - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python collections module - Collections in Python, Collections Counter, Python defaultdict, Python ordereddict, Python namedtuple with syntax &amp; examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-collections-module\/\" \/>\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-10T01:29:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T09:50:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Collections Module - Python Counter, DefaultDict, OrderedDict, NamedTuple - DataFlair","description":"Python collections module - Collections in Python, Collections Counter, Python defaultdict, Python ordereddict, Python namedtuple with syntax & examples","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-collections-module\/","og_locale":"en_US","og_type":"article","og_title":"Python Collections Module - Python Counter, DefaultDict, OrderedDict, NamedTuple - DataFlair","og_description":"Python collections module - Collections in Python, Collections Counter, Python defaultdict, Python ordereddict, Python namedtuple with syntax & examples","og_url":"https:\/\/data-flair.training\/blogs\/python-collections-module\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-10T01:29:04+00:00","article_modified_time":"2026-04-24T09:50:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Collections Module &#8211; Python Counter, DefaultDict, OrderedDict, NamedTuple","datePublished":"2018-02-10T01:29:04+00:00","dateModified":"2026-04-24T09:50:02+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/"},"wordCount":931,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg","keywords":["Collections module in Python","python collection modules","python collections","python collections tutorial","Python defaultdict","Python Namedtuple","Python namedtuple Accessing Elements","python ordereddict"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-collections-module\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/","url":"https:\/\/data-flair.training\/blogs\/python-collections-module\/","name":"Python Collections Module - Python Counter, DefaultDict, OrderedDict, NamedTuple - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg","datePublished":"2018-02-10T01:29:04+00:00","dateModified":"2026-04-24T09:50:02+00:00","description":"Python collections module - Collections in Python, Collections Counter, Python defaultdict, Python ordereddict, Python namedtuple with syntax & examples","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-collections-module\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Collections-Module-01-1.jpg","width":1200,"height":628,"caption":"Python Collections Module - Introduction"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-collections-module\/#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 Collections Module &#8211; Python Counter, DefaultDict, OrderedDict, NamedTuple"}]},{"@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\/7721","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=7721"}],"version-history":[{"count":15,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7721\/revisions"}],"predecessor-version":[{"id":147842,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7721\/revisions\/147842"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/42238"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}