

{"id":7729,"date":"2018-02-10T07:15:22","date_gmt":"2018-02-10T01:45:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=7729"},"modified":"2026-04-24T15:12:48","modified_gmt":"2026-04-24T09:42:48","slug":"python-counter","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-counter\/","title":{"rendered":"Python Counter with Example &amp; Python Collections Type"},"content":{"rendered":"<p>Earlier, we discussed three classes from Python module collections. Today, we\u2019ll talk about another such class- Python Counter. Here, we will study initializing, updating, accessing, and reassigning counters in Python.<\/p>\n<p>Moreover, we will learn Python counter list, loops and arithmetic.<\/p>\n<p>So, let&#8217;s start the Python Counter Tutorial.<\/p>\n<div id=\"attachment_7742\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7742\" class=\"wp-image-7742 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg\" alt=\"What is Python Counter\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-7742\" class=\"wp-caption-text\">Python Counter with Example &amp; Python Collections Type<\/p><\/div>\n<h3>What is Python Counter?<\/h3>\n<p>Python Counter, like the other three containers we mentioned above, is a subclass of \u2018dict\u2019. It keeps a count of the number of occurrences of any value in the container.<\/p>\n<p>Simply speaking, if you add the value \u2018hello\u2019 thrice in the container, it will remember that you added it thrice. So, Counter counts hashable objects\u00a0in\u00a0Python.<\/p>\n<p><strong> Why is the counter used over the dictionaries?<\/strong><\/p>\n<ul>\n<li>The counter in Python can handle the missing keys gracefully.<\/li>\n<li>If you ask for a count that is not available, then it returns 0 instead of crashing the program.<\/li>\n<\/ul>\n<p>Let&#8217;s see this Python Counter example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; from collections import Counter\r\n&gt;&gt;&gt; c=Counter(['a','b','c','a','b','a'])\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['a']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; issubclass(Counter,dict)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>Now, let\u2019s take a look at Python counter syntax.<\/p>\n<h3>Initializing a Python Counter<\/h3>\n<p>To initialize or define a counter in Python, we use the counter factory function. Yet, in that, we can do it in three ways:<\/p>\n<h4>1. Using a List or Similar Containers<\/h4>\n<p>We can pass a Python list of values to Counter(). Every time it encounters a value again, it raises its count by 1.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter(['a','b','c','a','b','a'])\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<p>As you can see, it could recognize that there are three \u2018a\u2019s in the list in Python, two \u2018b\u2019s, and one \u2018c\u2019.<br \/>\nWe can also use a Python tuple.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter(('a','b','c','a','b','a'))\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<p>Or, you can pass it a Python string.<\/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<p>Python string is a container too, remember? Also, it displays the counts in descending order.<\/p>\n<p>But when we use a Set, it only holds every value once. So, it does not make sense to use Counter() with Python Set.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; c=Counter({'a','b','c','a','b','a'})\r\n&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;a&#8217;: 1, &#8216;c&#8217;: 1, &#8216;b&#8217;: 1})<\/div>\n<h4>2. Using a Python Dictionary<\/h4>\n<p>We can also manually tell the Python Counter the count of values, using a dictionary in Python.<\/p>\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>3. Using Keyword Arguments<\/h4>\n<p>Finally, we can use keyword arguments to manually tell Counter() the count.<\/p>\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<h3>Updating a Python Counter<\/h3>\n<p>Like it is with every other container, we can first declare an empty Python Counter and then populate it. We perform the update calling the update() method on the Counter.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d=Counter()\r\n&gt;&gt;&gt; d.update(\"Hello\")\r\n&gt;&gt;&gt; d<\/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<p>We updated it once; we can update it further, in a different way if we want.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d.update({'e':2,'o':4})\r\n&gt;&gt;&gt; d<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;o&#8217;: 5, &#8216;e&#8217;: 3, &#8216;l&#8217;: 2, &#8216;H&#8217;: 1})<\/div>\n<p>Here, we told Python Counter that we\u2019re adding two more \u2018e\u2019s and four more \u2018o\u2019s. Then, when we accessed it, it printed the counts in descending order.<\/p>\n<h3>Accessing Counts in Python<\/h3>\n<p>We can access the count for a particular value simply by using it as an index to the Python Counter we defined.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; d['e']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>Let\u2019s try displaying the counts of values in a string, with Python counter that takes a different string as an argument.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e=Counter(\"Hello\")\r\n&gt;&gt;&gt; for i in \"Help\": \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n          print(f\"{i}: {e[i]}\") \u00a0 \u00a0 \u00a0\u00a0 \u00a0\r\nH: 1\r\ne: 1\r\nl: 2\r\np: 0<\/pre>\n<p>From this, we interpret that in Python Counter e, \u2018H\u2019 has a count of 1, \u2018e\u2019 has a count of 1, \u2018l\u2019 has 2, and \u2018p\u2019 has none(hence, 0).<\/p>\n<p>From this, we conclude that if a key doesn\u2019t exist, it will consider its count to be 0, instead of raising a KeyError.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e['q']<\/pre>\n<p>To compare, let\u2019s also see how this would fair in a regular dictionary.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; dict1={'H':1,'e':1,'l':2,'o':1}\r\n&gt;&gt;&gt; dict1['p']\r\nTraceback (most recent call last):\r\nFile \"&lt;pyshell#36&gt;\", line 1, in &lt;module&gt;\r\ndict1['p']<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">KeyError: &#8216;p&#8217;<\/div>\n<h4>1. The elements() Method<\/h4>\n<p>Or, we could use the elements() method, which returns an iterator object for the values in the Counter. We can use this with a Python for-loop.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e=Counter({'a':3,'b':2,'c':1,'d':0})\r\n&gt;&gt;&gt; for i in e.elements():\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(f\"{i}: {e[i]}\")\r\na: 3\r\na: 3\r\na: 3\r\nb: 2\r\nb: 2\r\nc: 1\r\n&gt;&gt;&gt; e.elements()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>&lt;itertools.chain object at 0x06336390&gt;<\/p>\n<\/div>\n<p>We\u2019ll discuss python itertools in a later lesson.<\/p>\n<h4>2. Accessing the Most Common Values<\/h4>\n<p>To get n most-common values (the ones with the highest frequencies), we call the method most_common().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e.most_common(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[(&#8216;a&#8217;, 3), (&#8216;b&#8217;, 2)]<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e.most_common(4)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[(&#8216;a&#8217;, 3), (&#8216;b&#8217;, 2), (&#8216;c&#8217;, 1), (&#8216;d&#8217;, 0)]<\/p>\n<\/div>\n<p>When we call it without any argument, however, we get all the items. So, it is the same as calling it with an argument with a value equal to the number of elements in Python counter.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e.most_common()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[(&#8216;a&#8217;, 3), (&#8216;b&#8217;, 2), (&#8216;c&#8217;, 1), (&#8216;d&#8217;, 0)]<\/p>\n<\/div>\n<h3>Reassigning Counts in Python<\/h3>\n<p>Of course, Python Counters aren\u2019t immutable. You can reassign a count in the following way:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; e['e']=5\r\n&gt;&gt;&gt; e<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;e&#8217;: 5, &#8216;l&#8217;: 2, &#8216;H&#8217;: 1, &#8216;o&#8217;: 1})<\/p>\n<\/div>\n<p>To clear a Python counter, we use clear().<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; b.clear()\r\n&gt;&gt;&gt; b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter()<\/p>\n<\/div>\n<h3>Python Counter Arithmetic<\/h3>\n<p>Finally, we\u2019ll discuss some Counter arithmetic in Python.<\/p>\n<p><strong>Operators that can be used in counter arithmetic are:<\/strong><\/p>\n<ul>\n<li><strong>Addition(+):<\/strong> It adds the counts from both the counters.<\/li>\n<li><strong>Subtraction(-):<\/strong> It gives out only positive results.<\/li>\n<li><strong>Intersection(&amp;):<\/strong> It returns the minimum count of the common elements.<\/li>\n<li><strong>Union(|):<\/strong> It returns the maximum count of the common elements.<\/li>\n<\/ul>\n<p>To aggregate results, we can perform arithmetic and set operations on Python Counters. Let\u2019s take two sample Counters for exemplary purposes.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=Counter({'a':3,'b':2,'c':1})\r\n&gt;&gt;&gt; b=Counter({'c':3,'d':2,'e':1})<\/pre>\n<p>Now, sit back and watch it unfold.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a+b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;c&#8217;: 4, &#8216;a&#8217;: 3, &#8216;b&#8217;: 2, &#8216;d&#8217;: 2, &#8216;e&#8217;: 1})<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a-b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;a&#8217;: 3, &#8216;b&#8217;: 2})<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; b-a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;c&#8217;: 2, &#8216;d&#8217;: 2, &#8216;e&#8217;: 1})<\/p>\n<\/div>\n<p>Here, a-b does not have \u2018c\u2019, because 1-3=-2. A negative count does not mean anything. Likewise, b-a has \u2018c\u2019 with a count of 2, because 3-1=2. We can also do this by calling the subtract() method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a.subtract(b)\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;a&#8217;: 3, &#8216;b&#8217;: 2, &#8216;e&#8217;: -1, &#8216;c&#8217;: -2, &#8216;d&#8217;: -2})<\/div>\n<p>This way, it gives us negative counts if it has to.<br \/>\nNow, look at the following code:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a.__add__(b)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;a&#8217;: 3, &#8216;b&#8217;: 2, &#8216;c&#8217;: 1})<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a&amp;b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;c&#8217;: 1})<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a|b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Counter({&#8216;a&#8217;: 3, &#8216;c&#8217;: 3, &#8216;b&#8217;: 2, &#8216;d&#8217;: 2, &#8216;e&#8217;: 1})<\/p>\n<\/div>\n<p>Here, a|b has a count of 3 for \u2018c\u2019, because that is what OR does.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; a=Counter({'e':-1})\r\n&gt;&gt;&gt; -a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Counter({&#8216;e&#8217;: 1})<\/div>\n<p>If you\u2019ve forgotten about these operators, read up on Python Operators.<br \/>\nCheck out the result for dir(Counter), and try to apply all of those methods to your Counters in Python.<\/p>\n<h3>Python Interview Questions on Python Counter<\/h3>\n<ol>\n<li>What is a counter in Python?<\/li>\n<li>What does counter () do in Python?<\/li>\n<li>How do you write a count function in Python?<\/li>\n<li>How do you increment a counter in Python?<\/li>\n<li>How do you make a time counter in Python?<\/li>\n<\/ol>\n<h3>Conclusion<\/h3>\n<p>Python Counter is a container that keeps track of the number of occurrences of a value. Today, we looked at methods like update(), most_common(), clear(), elements(), and subtract().<\/p>\n<p>In our next lesson, we\u2019ll revise all four classes in brief.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, we discussed three classes from Python module collections. Today, we\u2019ll talk about another such class- Python Counter. Here, we will study initializing, updating, accessing, and reassigning counters in Python. Moreover, we will learn&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":7742,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[222,3015,6715,10427,10450,10451,10686,11418,15175,16511],"class_list":["post-7729","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-accessing-counts-in-python","tag-counter-arithmetic-in-python","tag-initializing-a-python-counter","tag-python-collection-module","tag-python-counter","tag-python-counter-arithmetic","tag-python-module","tag-reassigning-counts-in-python","tag-updating-a-python-counter","tag-using-python-dictionary"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Counter with Example &amp; Python Collections Type - DataFlair<\/title>\n<meta name=\"description\" content=\"Let&#039;s study Python counters, how to initialize, update, access, and reassign counters in Python, and learn about counter lists, loops, and arithmetic.\" \/>\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-counter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Counter with Example &amp; Python Collections Type - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Let&#039;s study Python counters, how to initialize, update, access, and reassign counters in Python, and learn about counter lists, loops, and arithmetic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-counter\/\" \/>\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:45:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-24T09:42:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-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 Counter with Example &amp; Python Collections Type - DataFlair","description":"Let's study Python counters, how to initialize, update, access, and reassign counters in Python, and learn about counter lists, loops, and arithmetic.","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-counter\/","og_locale":"en_US","og_type":"article","og_title":"Python Counter with Example &amp; Python Collections Type - DataFlair","og_description":"Let's study Python counters, how to initialize, update, access, and reassign counters in Python, and learn about counter lists, loops, and arithmetic.","og_url":"https:\/\/data-flair.training\/blogs\/python-counter\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-02-10T01:45:22+00:00","article_modified_time":"2026-04-24T09:42:48+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-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-counter\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Counter with Example &amp; Python Collections Type","datePublished":"2018-02-10T01:45:22+00:00","dateModified":"2026-04-24T09:42:48+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/"},"wordCount":1073,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg","keywords":["Accessing Counts in python","Counter Arithmetic in Python","Initializing a Python Counter","python collection module","python counter","Python Counter Arithmetic","python module","Reassigning Counts in Python","Updating a Python Counter","using Python dictionary"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-counter\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-counter\/","url":"https:\/\/data-flair.training\/blogs\/python-counter\/","name":"Python Counter with Example &amp; Python Collections Type - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg","datePublished":"2018-02-10T01:45:22+00:00","dateModified":"2026-04-24T09:42:48+00:00","description":"Let's study Python counters, how to initialize, update, access, and reassign counters in Python, and learn about counter lists, loops, and arithmetic.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-counter\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/02\/Python-Counter-01.jpg","width":1200,"height":628,"caption":"What is Python Counter"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-counter\/#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 Counter with Example &amp; Python Collections Type"}]},{"@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\/7729","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=7729"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7729\/revisions"}],"predecessor-version":[{"id":147840,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/7729\/revisions\/147840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/7742"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=7729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=7729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=7729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}