{"id":801,"date":"2022-03-20T21:06:00","date_gmt":"2022-03-20T15:36:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=801"},"modified":"2023-08-15T16:53:59","modified_gmt":"2023-08-15T11:23:59","slug":"python-enumerate-function-with-example-beginners-guide","status":"publish","type":"post","link":"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide","title":{"rendered":"Python enumerate() Function With Example &#8211; Beginner&#8217;s Guide"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"heading-introduction\">Introduction<\/h1>\n\n\n\n<p>First of all, What is the meaning of&nbsp;<strong>enumerating<\/strong>?<\/p>\n\n\n\n<p>The meaning of enumerating is&nbsp;<strong>to number or name a list of things separately<\/strong>.<\/p>\n\n\n\n<p>Python&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function does the same thing as its name depicts itself.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-python-enumerate\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-python-enumerate\"><\/a>Python enumerate()<\/h1>\n\n\n\n<p>Python&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function takes a collection(e.g. a&nbsp;<strong>list<\/strong>&nbsp;or a&nbsp;<strong>tuple<\/strong>) and returns them as an enumerated object.<\/p>\n\n\n\n<p>The <code><strong>enumerate()<\/strong><\/code>&nbsp;function adds a number\/count to each item of the iterable object and returns an enumerate object.<\/p>\n\n\n\n<p><strong>Here&#8217;s a simple example of<\/strong>&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;<strong>function usage:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = ('Welcome', 'to', 'GeekPython')\n\noutput = (enumerate(greet, start=0))\n\n# Converted enumerated object to list\nprint(list(output))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">[(0, 'Welcome'), (1, 'to'), (2, 'GeekPython')]<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"heading-syntax\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-syntax\"><\/a>Syntax<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>The syntax of<\/strong>&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;<strong>function is:<\/strong><\/p>\n\n\n\n<p><code><strong>enumerate(iterable, start=0)<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-parameters\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-parameters\"><\/a><\/h3>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-parameters\">Parameters<\/h3>\n\n\n\n<p><code><strong>iterable<\/strong><\/code>&nbsp;&#8211; must be a sequence, an iterator, or objects that supports iteration.<\/p>\n\n\n\n<p><code><strong>start<\/strong><\/code>&nbsp;&#8211; It is optional. If the&nbsp;<code><strong>start<\/strong><\/code>&nbsp;is not specified then the count will start from&nbsp;<strong>0(default)<\/strong>&nbsp;else the count will start from a specified number and increment until the loop ends.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = ('Welcome', 'to', 'GeekPython')\n\noutput = (enumerate(greet, start=10))\n\n# Converted enumerated object to list\nprint(list(output))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">[(10, 'Welcome'), (11, 'to'), (12, 'GeekPython')]<\/pre><\/div>\n\n\n\n<p>We specified the&nbsp;<code><strong>start<\/strong><\/code>&nbsp;parameter which is equal to 10 and hence the enumerated output starts from the specified number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-fact\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-fact\"><\/a>Fact<\/h3>\n\n\n\n<p>If we see the above example, we converted our enumerate object into the list type and then print it.<\/p>\n\n\n\n<p>What if we don&#8217;t convert our enumerated object into a list type then what will happen?<\/p>\n\n\n\n<p>Well, the answer is pretty simple&nbsp;<strong>we don&#8217;t get any enumerated object<\/strong>&nbsp;instead,&nbsp;<strong>we&#8217;ll get the<\/strong> <strong>location of an<\/strong> <strong>enumerating object in the memory\/CPU<\/strong>.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = ('Welcome', 'to', 'GeekPython')\n\noutput = (enumerate(greet))\nprint(output)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">&lt;enumerate object at 0x0000016677A25E40&gt;<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-looping-over-enumerate-object\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-looping-over-enumerate-object\"><\/a>Looping over enumerate() object<\/h1>\n\n\n\n<p>We can perform&nbsp;<code><strong>for<\/strong><\/code>&nbsp;loop to iterate over enumerate objects.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">my_obj = [\"Python\", \"Ruby\", \"JavaScript\", \"Java\"]\n\nprint(\"Looping over enumerate object:\")\nfor item in enumerate(my_obj):\n    print(item)\n\nprint(\"\\nLooping over enumerate object using start value:\")\nfor item in enumerate(my_obj, start=5):\n    print(item)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Looping over enumerate object:\n(0, 'Python')\n(1, 'Ruby')\n(2, 'JavaScript')\n(3, 'Java')\n\nLooping over enumerate object using start value:\n(5, 'Python')\n(6, 'Ruby')\n(7, 'JavaScript')\n(8, 'Java')<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-performing-enumerate-on\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-performing-enumerate-on\"><\/a>Performing enumerate() on<\/h1>\n\n\n\n<p>We are going to see the examples where we use the&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function on different data types in Python.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-string\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-string\"><\/a>String &#8211;<\/h3>\n\n\n\n<p>Python&nbsp;<code><strong>str<\/strong><\/code>&nbsp;type can be iterated so we can perform&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function on it.<\/p>\n\n\n\n<p><strong>Example: Performing enumerate() on string<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">text = \"GeekPython\"\n\n# Using loop to iterate and enumerating\nprint(\"Using loop to iterate and enumerating\")\nfor word in enumerate(text):\n    print(word)\n\nprint(\"----------------xxx--------------------\")\n\n# Using simple approach\nprint(\"Using simple approach\")\noutput = enumerate(text)\nprint(list(output))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Using loop to iterate and enumerating\n(0, 'G')\n(1, 'e')\n(2, 'e')\n(3, 'k')\n(4, 'P')\n(5, 'y')\n(6, 't')\n(7, 'h')\n(8, 'o')\n(9, 'n')\n----------------xxx--------------------\nUsing simple approach\n[(0, 'G'), (1, 'e'), (2, 'e'), (3, 'k'), (4, 'P'), (5, 'y'), (6, 't'), (7, 'h'), (8, 'o'), (9, 'n')]<\/pre><\/div>\n\n\n\n<p>We can use&nbsp;<code><strong>start<\/strong><\/code>&nbsp;parameter also &#8211;<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">text = \"GeekPython\"\n\nprint(\"Using loop to iterate and enumerating using specified number:\")\nfor word in enumerate(text, start=10):\n    print(word)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Using loop to iterate and enumerating using specified number:\n(10, 'G')\n(11, 'e')\n(12, 'e')\n(13, 'k')\n(14, 'P')\n(15, 'y')\n(16, 't')\n(17, 'h')\n(18, 'o')\n(19, 'n')<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-list\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-list\"><\/a>List &#8211;<\/h3>\n\n\n\n<p>We performed&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function on&nbsp;<code><strong>list<\/strong><\/code>&nbsp;type in the example below where&nbsp;the <strong>first block of code gives<\/strong> <strong>the start value as 2<\/strong>&nbsp;and we used&nbsp;a <strong>simple approach in the second block of code<\/strong>.<\/p>\n\n\n\n<p><strong>Example: Performing enumerate() on list<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = ['Welcome', 'to', 'GeekPython']\n\nprint(\"Using enumerate on list using start:\")\nfor iterables in enumerate(greet, start=2):\n    print(iterables)\n\nprint(\"\\nUsing enumerate on list:\")\noutput = enumerate(greet)\nprint(list(output))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Using enumerate on list using start:\n(2, 'Welcome')\n(3, 'to')\n(4, 'GeekPython')\n\nUsing enumerate on list:\n[(0, 'Welcome'), (1, 'to'), (2, 'GeekPython')]<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-tuple\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-tuple\"><\/a>Tuple &#8211;<\/h3>\n\n\n\n<p><strong>Example: Performing enumerate() on tuple<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = ('Welcome', 'to', 'GeekPython')\n\nprint(\"Using enumerate on tuple:\")\nfor item in enumerate(greet):\n    print(item)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Using enumerate on tuple:\n(0, 'Welcome')\n(1, 'to')\n(2, 'GeekPython')<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-dictionary\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-dictionary\"><\/a>Dictionary &#8211;<\/h3>\n\n\n\n<p><code><strong>dict()<\/strong><\/code>&nbsp;type contains&nbsp;<strong>key-value<\/strong>&nbsp;pairs, so we can&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;dictionary items (keys and values) separately.<\/p>\n\n\n\n<p><strong>Example: Performing enumerate() on dictionary<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">values = {\"a\" : \"Geek\", \"b\" : \"Python\"}\n\nprint(\"Accessing values from dict and enumerating:\")\nfor value in enumerate(values.values()):\n    print(value)\n\nprint(\"\\nAccessing keys from dict and enumerating:\")\nfor key in enumerate(values.keys()):\n    print(key)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Accessing values from dict and enumerating:\n(0, 'Geek')\n(1, 'Python')\n\nAccessing keys from dict and enumerating:\n(0, 'a')\n(1, 'b')<\/pre><\/div>\n\n\n\n<p><strong>Example: Performing enumerate() on dictionary using start<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">values = {\"a\" : \"Geek\", \"b\" : \"Python\"}\n\nprint(\"Accessing values from dict and enumerating using start:\")\nfor value in enumerate(values.values(), start=5):\n    print(value)\n\nprint(\"\\nAccessing keys from dict and enumerating using start:\")\nfor key in enumerate(values.keys(), start=7):\n    print(key)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Accessing values from dict and enumerating using start:\n(5, 'Geek')\n(6, 'Python')\n\nAccessing keys from dict and enumerating using start:\n(7, 'a')\n(8, 'b')<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-set\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-set\"><\/a>Set &#8211;<\/h3>\n\n\n\n<p><code><strong>set()<\/strong><\/code>&nbsp;type in Python is&nbsp;<strong>unordered, unchangeable, and unindexed<\/strong>, so&nbsp;<strong>when we enumerate them, the &#8220;set&#8221; item appears in random order<\/strong>.<\/p>\n\n\n\n<p><strong>Example: Performing enumerate() on set<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">greet = {'Welcome', 'to', 'GeekPython'}\n\nprint(\"Using enumerate on set:\")\nfor item in enumerate(greet):\n    print(item)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Using enumerate on set:\n(0, 'GeekPython')\n(1, 'to')\n(2, 'Welcome')<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/python-enumerate-function-with-example-beginners-guide#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>As we discussed above, Python&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function helps us to add counter to our iterable objects.<\/p>\n\n\n\n<p>You have seen the implementation of&nbsp;<code><strong>enumerate()<\/strong><\/code>&nbsp;function on the built-in data types in Python as well as you&#8217;ve seen how you can loop over iterable objects and enumerate them.<\/p>\n\n\n\n<p>You can use this function in many ways like&nbsp;<strong>enumerating the files in the particular directory to make the folder structure pretty<\/strong>&nbsp;or you can&nbsp;<strong>make a tool where a user can automatically number their files and folders easily<\/strong>&nbsp;or maybe something else.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now.<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction First of all, What is the meaning of&nbsp;enumerating? The meaning of enumerating is&nbsp;to number or name a list of things separately. Python&nbsp;enumerate()&nbsp;function does the same thing as its name depicts itself. Python enumerate() Python&nbsp;enumerate()&nbsp;function takes a collection(e.g. a&nbsp;list&nbsp;or a&nbsp;tuple) and returns them as an enumerated object. The enumerate()&nbsp;function adds a number\/count to each item [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":805,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,41],"tags":[40,12,31],"class_list":["post-801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-function","tag-functions","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":5,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":1375,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/801\/revisions\/1375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/805"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}