{"id":597,"date":"2024-10-19T07:24:48","date_gmt":"2024-10-19T07:24:48","guid":{"rendered":"https:\/\/itsmycode.com\/?p=597"},"modified":"2024-10-19T07:24:49","modified_gmt":"2024-10-19T07:24:49","slug":"python-list-count","status":"publish","type":"post","link":"https:\/\/itsmycode.com\/python-list-count\/","title":{"rendered":"Python List count()"},"content":{"rendered":"\n<p><strong>Python List count()&nbsp;<\/strong>is a built-in function that returns the number of times the specified element occurs in the list.<\/p>\n\n\n\n<p>In this tutorial, we will learn about the&nbsp;<strong>Python list count()<\/strong>&nbsp;method with the help of examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax-of-list-count\">Syntax of List count()&nbsp;<\/h2>\n\n\n\n<p>The syntax of the&nbsp;<strong><code>count()<\/code><\/strong>&nbsp;method is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list.count(element)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"count-parameters\">count() Parameters<\/h2>\n\n\n\n<p>The&nbsp;<strong><code>count()<\/code><\/strong>&nbsp;method takes a single parameter.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>element \u2013\u00a0<\/strong>The element or value that needs to be counted in the list.<\/li>\n<\/ul>\n\n\n\n<p>If more than one element is passed to the count() method, it will throw&nbsp;<strong><code>TypeError: count() takes exactly one argument (2 given)<\/code><\/strong> exception.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"return-value-from-list-count\">Return Value from List count()<\/h2>\n\n\n\n<p>The&nbsp;<strong><code>count()<\/code><\/strong>&nbsp;method returns the number of times an element has appeared in the list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-1-use-of-count\">Example 1: Use of count()<\/h2>\n\n\n\n<p>In the below example the <strong>count()<\/strong> method returns the number of times the elements appears in the list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># list of cars\ncars = &#91;'Benz','BMW','Ford','Ferrari','volkswagen','BMW']\nnumbers= &#91;1,5,2,5,7,1,3,3,1,6]\n\n# BWM Count in list\nbmwCount = cars.count('BMW')\nprint(\"total no BMW count is = \",bmwCount)\n\n# number count in list\nnumCount = numbers.count(1)\nprint(\"The count of number 1 is = \",numCount)\n\n# if you give number in string format\nnumCount = numbers.count('3')\nprint(\"The count of number 3 is= \",numCount)<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total no BMW count is =  2\nThe count of number 1 is =  3\nThe count of number 3 is=  0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-count-tuple-and-list-elements-inside-list\">Example 2: Count Tuple and List Elements Inside List<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># list of cars\ncars = &#91;'Benz',('volkswagen','BMW'),'Ford','Ferrari',('volkswagen','BMW')]\nnumbers= &#91;1,(1,3),5,7,(1,3),3,1,6,(1,3)]\n\n# BWM Count in list\nbmwCount = cars.count(('volkswagen','BMW'))\nprint(\"total no BMW, volkswagen count is = \",bmwCount)\n\n# number count in list\nnumCount = numbers.count((1,3))\nprint(\"The count of number 1,3  is = \",numCount)\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total no BMW, volkswagen count is =  2\nThe count of number 1,3  is =  3<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python List count()&nbsp;is a built-in function that returns the number of times the specified element occurs in the list. In this tutorial, we will learn about the&nbsp;Python list count()&nbsp;method with the help of examples. Syntax of List count()&nbsp; The syntax of the&nbsp;count()&nbsp;method is: list.count(element) count() Parameters The&nbsp;count()&nbsp;method takes a single parameter. If more than one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":598,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,1],"tags":[],"class_list":["post-597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list-methods","category-python"],"_links":{"self":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":1,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/posts\/597\/revisions\/599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media\/598"}],"wp:attachment":[{"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itsmycode.com\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}