{"id":434,"date":"2017-09-13T15:42:27","date_gmt":"2017-09-13T20:42:27","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=434"},"modified":"2017-09-13T16:14:55","modified_gmt":"2017-09-13T21:14:55","slug":"python-how-to-use-enumerate","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/13\/python-how-to-use-enumerate\/","title":{"rendered":"Python: How to use enumerate( )"},"content":{"rendered":"<p>Python&#8217;s built-in enumerate function takes in any iterable sequence, such as a list, and yields the elements of the sequence along with an index number. This is useful for adding a count to every element of a list. The basic syntax of this function is:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">enumerate(sequence, start=0)<\/code><\/p>\n<p>where <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">sequence<\/code> is the list or iterable data container and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">start=0<\/code> is what you want the counter to start at.<\/p>\n<p>Without enumerate, we can print the the elements of a list and number them via a for loop by setting up and incrementing a counter. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_list = ['apple', 'banana', 'cherry']\r\n\r\ncounter = 1\r\nfor item in fruit_list:\r\n    print '{0}. {1}'.format(counter, item)\r\n    counter += 1<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">1. apple\r\n2. banana\r\n3. cherry<\/pre>\n<p>Using enumerate, we can do the same thing, but without setting up and incrementing a counter:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">fruit_list = ['apple', 'banana', 'cherry']\r\n\r\nfor counter, item in enumerate(fruit_list, start=1):\r\n    print '{0}. {1}'.format(counter, item)<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">1. apple\r\n2. banana\r\n3. cherry<\/pre>\n<p><strong>Here are some other use cases:<\/strong><\/p>\n<p>Using enumerate to create a list of tuples:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">print list(enumerate(fruit_list))<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">[(0, 'apple'), (1, 'banana'), (2, 'cherry')]<\/pre>\n<p>Using enumerate to create a dictionary of key-value pairs:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">print dict(enumerate(fruit_list))<\/pre>\n<p>Which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">{0: 'apple', 1: 'banana', 2: 'cherry'}<\/pre>\n<p>For more information see:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/functions.html#enumerate\">https:\/\/docs.python.org\/2\/library\/functions.html#enumerate<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s built-in enumerate function takes in any iterable sequence, such as a list, and yields the elements of the sequence along with an index number. This is useful for adding a count to every element of a list. The basic syntax of this function is: enumerate(sequence, start=0) where sequence is the list or iterable data &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/09\/13\/python-how-to-use-enumerate\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to use enumerate( )<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[32,4],"class_list":["post-434","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-enumerate","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":9,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/434\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}