{"id":4735,"date":"2022-09-13T01:45:25","date_gmt":"2022-09-13T01:45:25","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4735"},"modified":"2022-09-13T02:15:20","modified_gmt":"2022-09-13T02:15:20","slug":"python-len","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-len\/","title":{"rendered":"Python len()"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Python <code>len()<\/code> function to get the number of items of an object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-len-function'>Introduction to the Python len() function <a href=\"#introduction-to-the-python-len-function\" class=\"anchor\" id=\"introduction-to-the-python-len-function\" title=\"Anchor for Introduction to the Python len() function\">#<\/a><\/h2>\n\n\n\n<p>The <code><code>len()<\/code><\/code> function returns the number of items  (length) of an object. Here&#8217;s the syntax of the <code><code>len()<\/code><\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">len(s)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, the s is an object that you want to get the length. The object can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A sequence such as a string, bytes, tuple, list or range.<\/li><li>A collection such as a dictionary, a set or a frozen set.<\/li><li>Or any user-defined object that implements the <code>__len__()<\/code> method.<\/li><\/ul>\n\n\n\n<p>When you call the <code><code>len()<\/code><\/code> function on an object,  the <code><code>len()<\/code><\/code> function will delegate to __len__ method of the object. In other words, it&#8217;ll call the <code>__len__()<\/code> method of the object.<\/p>\n\n\n\n<p>So the following function call:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">len(obj)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>is equivalent to:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">obj.__len__()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-len-function-examples'>Python len() function examples <a href=\"#python-len-function-examples\" class=\"anchor\" id=\"python-len-function-examples\" title=\"Anchor for Python len() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using Python <code>len()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-the-len-function-with-a-sequence'>1) Using the len() function with a sequence <a href=\"#1-using-the-len-function-with-a-sequence\" class=\"anchor\" id=\"1-using-the-len-function-with-a-sequence\" title=\"Anchor for 1) Using the len() function with a sequence\">#<\/a><\/h3>\n\n\n\n<p>The following example shows how to use the <code>len()<\/code> function to get the length of a string, the number of elements in a tuple, and the number of items in a range:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">s = <span class=\"hljs-string\">'Python'<\/span>\nprint(len(s))  <span class=\"hljs-comment\"># ? 6<\/span>\n\nseasons = (<span class=\"hljs-string\">'Spring'<\/span>, <span class=\"hljs-string\">'Summer'<\/span>, <span class=\"hljs-string\">'Autumn'<\/span>, <span class=\"hljs-string\">'Winter'<\/span>)\nprint(len(seasons))  <span class=\"hljs-comment\"># ? 4<\/span>\n\n\nr = range(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>)\nprint(len(r))  <span class=\"hljs-comment\"># ? 9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-the-len-function-with-a-collection'>2) Using the len() function with a collection <a href=\"#2-using-the-len-function-with-a-collection\" class=\"anchor\" id=\"2-using-the-len-function-with-a-collection\" title=\"Anchor for 2) Using the len() function with a collection\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code>len()<\/code> function to get the number of items in a collection:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">days = {\n    <span class=\"hljs-number\">1<\/span>: <span class=\"hljs-string\">'Sun'<\/span>,\n    <span class=\"hljs-number\">2<\/span>: <span class=\"hljs-string\">'Mon'<\/span>,\n    <span class=\"hljs-number\">3<\/span>: <span class=\"hljs-string\">'Tue'<\/span>,\n    <span class=\"hljs-number\">4<\/span>: <span class=\"hljs-string\">'Wed'<\/span>,\n    <span class=\"hljs-number\">5<\/span>: <span class=\"hljs-string\">'Thu'<\/span>,\n    <span class=\"hljs-number\">6<\/span>: <span class=\"hljs-string\">'Fri'<\/span>,\n    <span class=\"hljs-number\">7<\/span>: <span class=\"hljs-string\">'Sat'<\/span>\n}\nprint(len(days))  <span class=\"hljs-comment\"># ? 7<\/span>\n\nrgb = set((<span class=\"hljs-string\">'red'<\/span>, <span class=\"hljs-string\">'green'<\/span>, <span class=\"hljs-string\">'blue'<\/span>))\nprint(len(rgb))  <span class=\"hljs-comment\"># ? 3<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-using-the-len-function-with-user-defined-object'>3) Using the len() function with user-defined object <a href=\"#3-using-the-len-function-with-user-defined-object\" class=\"anchor\" id=\"3-using-the-len-function-with-user-defined-object\" title=\"Anchor for 3) Using the len() function with user-defined object\">#<\/a><\/h3>\n\n\n\n<p>The following example shows how to use the <code>len()<\/code> function with a user-defined object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Employee<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n        self.dependents = &#91;]\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_dependent<\/span><span class=\"hljs-params\">(self, dependent)<\/span>:<\/span>\n        self.dependents.append(dependent)\n        <span class=\"hljs-keyword\">return<\/span> self\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> len(self.dependents)\n\n\nemployee = Employee(<span class=\"hljs-string\">'John Doe'<\/span>)\nemployee.add_dependent(<span class=\"hljs-string\">'Alice Doe'<\/span>)\nemployee.add_dependent(<span class=\"hljs-string\">'Bob Doe'<\/span>)\n\nprint(len(employee))  <span class=\"hljs-comment\"># ? 2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define the Employee class that has two attributes name and dependents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Employee<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, name)<\/span>:<\/span>\n        self.name = name\n        self.dependents = &#91;]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, define the <code>add_dependent()<\/code> that adds a dependent to the dependents list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_dependent<\/span><span class=\"hljs-params\">(self, dependent)<\/span>:<\/span>\n   self.dependents.append(dependent)\n   <span class=\"hljs-keyword\">return<\/span> self<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, implement the <code>__len__()<\/code> method that returns the number of dependents of the employee.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n   <span class=\"hljs-keyword\">return<\/span> len(self.dependents)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After that, create an Employee object and add two dependents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">employee = Employee(<span class=\"hljs-string\">'John Doe'<\/span>)\nemployee.add_dependent(<span class=\"hljs-string\">'Alice Doe'<\/span>)\nemployee.add_dependent(<span class=\"hljs-string\">'Bob Doe'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, pass the employee object to the <code><code>len()<\/code><\/code> function. The <code><code>len()<\/code><\/code> will call the <code>__len__()<\/code> method of the Employee class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>len()<\/code> function to get the number of items of an object.<\/li><li>The <code>len()<\/code> function delegates the call to <code>__len__()<\/code> method.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4735\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-len\/\"\n\t\t\t\tdata-post-title=\"Python len()\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4735\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-len\/\"\n\t\t\t\tdata-post-title=\"Python len()\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Python len() function to get the number of items of an object.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1055,"menu_order":13,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4735","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4735"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4735\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1055"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}