{"id":1529,"date":"2023-02-01T20:00:00","date_gmt":"2023-02-01T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1529"},"modified":"2023-01-15T15:32:45","modified_gmt":"2023-01-15T06:32:45","slug":"enumerate","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/enumerate\/","title":{"rendered":"[Python] How to Use enumerate() Function"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">how to use enumerate() function<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>What is enumerate function?<\/li><li>Basic usage of enumerate function<\/li><li>Start index from 1 (non-zero)<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What is enumerate function?<\/h2>\n\n\n\n<p>In the beginning, <span class=\"st-mymarker-s\">I explain briefly enumerate( ) function.<\/span><\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The enumerate() function is built-in function in Python.<\/p>\n\n\n\n<p>So, We can use enumerate( ) function without &#8220;import&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By using the enumerate( ) function, the elements and indices of iterable objects (such as list, tuple, etc.) are got simultaneously.<\/p>\n\n\n\n<p>The enumerate( ) function is often used with the &#8220;for loop&#8221;.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Basic usage of enumerate function<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">basic usage of enumerate( ) function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The enumerate( ) function takes iterables (such as list, tuple, etc.) as the arguments.<\/p>\n\n\n\n<p>If you want the beginning of the index to be non-zero, specify the argument &#8220;start&#8221;.<\/p>\n\n\n\n<p>The argument &#8220;start&#8221; is optional.<\/p>\n\n\n<div class=\"st-mybox  has-title st-mybox-class\" style=\"background:#E3F2FD;border-color:#2196F3;border-width:3px;border-radius:5px;margin: 25px 0 25px 0;\"><p class=\"st-mybox-title\" style=\"color:#757575;font-weight:bold;text-shadow: #fff 3px 0px 0px, #fff 2.83487px 0.981584px 0px, #fff 2.35766px 1.85511px 0px, #fff 1.62091px 2.52441px 0px, #fff 0.705713px 2.91581px 0px, #fff -0.287171px 2.98622px 0px, #fff -1.24844px 2.72789px 0px, #fff -2.07227px 2.16926px 0px, #fff -2.66798px 1.37182px 0px, #fff -2.96998px 0.42336px 0px, #fff -2.94502px -0.571704px 0px, #fff -2.59586px -1.50383px 0px, #fff -1.96093px -2.27041px 0px, #fff -1.11013px -2.78704px 0px, #fff -0.137119px -2.99686px 0px, #fff 0.850987px -2.87677px 0px, #fff 1.74541px -2.43999px 0px, #fff 2.44769px -1.73459px 0px, #fff 2.88051px -0.838246px 0px;background: linear-gradient(0deg,#E3F2FD 0%,#E3F2FD 55%,rgba(0,0,0,0) 55%,rgba(0,0,0,0) 100%);\">How to write<\/p><div class=\"st-in-mybox\">\n<p>enumerate( iterator, start = &#8220;number&#8221; )<\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\n\nfruits_enumerate = enumerate(fruits)\n\nprint(fruits_enumerate)\n# &lt;enumerate object at 0x0000023459307708&gt;\nprint(list(fruits_enumerate))\n# &#091;(0, 'Apple'), (1, 'Banana'), (2, 'Orange'), (3, 'Grape'), (4, 'Lemon')]<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; print(fruits_enumerate)<br>&gt; # &lt;enumerate object at 0x0000023459307708&gt;<\/p>\n\n\n\n<p>An enumerate object is generated.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&gt; print(list(fruits_enumerate))<\/p>\n\n\n\n<p>Since the elements cannot be checked as an enumerate object, they are converted to list type with the list( ) function.<\/p>\n\n\n\n<p>The indices and elements are stored in the tuples.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h3 class=\"wp-block-heading\">Use the enumerate( ) function with &#8220;for loop&#8221;<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The enumerate( ) function is often used with &#8220;for loop&#8221;<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\n\nfor i, fruit in enumerate(fruits):\n    print(i, fruit)\n# 0 Apple\n# 1 Banana\n# 2 Orange\n# 3 Grape\n# 4 Lemon<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>By using the enumerate( ) function, the indices and elements are passed to the variable i and fruit.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Start index from 1 (non-zero)<\/h2>\n\n\n\n<p>If you want the beginning of the index to be non-zero, specify the argument &#8220;start&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here is an example code.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>fruits = &#091;'Apple', 'Banana', 'Orange', 'Grape', 'Lemon']\n\nfor i, fruit in enumerate(fruits, start=1):\n    print(i, fruit)\n# 1 Apple\n# 2 Banana\n# 3 Orange\n# 4 Grape\n# 5 Lemon<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to use enumerate() function. What is enumerate function? In the beginning, I  &#8230; <\/p>\n","protected":false},"author":1,"featured_media":2705,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,9],"tags":[28,16],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1529"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":61,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":3620,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1529\/revisions\/3620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/2705"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}