{"id":24813,"date":"2021-11-02T09:48:02","date_gmt":"2021-11-02T04:18:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=24813"},"modified":"2021-11-05T20:36:54","modified_gmt":"2021-11-05T15:06:54","slug":"python-enumerate-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-enumerate-function-with-examples\/","title":{"rendered":"Python enumerate() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python divmod() Method with Examples<\/a> The enumerate() function accepts a collection (for example, a tuple) and returns an enumerate object.<\/p>\n The enumerate() function adds a counter as the enumerate object’s key.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values<\/strong><\/p>\n iterable:<\/strong> It is an iterable object.<\/p>\n start:<\/strong> This is optional. It is a number.\u00a0Specifying the enumerate object’s starting number. 0 is the default.<\/p>\n Return Value:<\/strong><\/p>\n The method enumerate() adds a counter to an iterable and returns it. The object returned is an enumerate object.<\/p>\n Enumerate objects can be converted to list and tuple using the list() and tuple() methods, respectively.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Fed up with searching various pages for the list of Python Built in Functions<\/a>? Look at the tutorial linked here and explore all coding samples of built-in functions of python.<\/p>\n In the previous article, we have discussed Python divmod() Method with Examples enumerate() Function in Python: The enumerate() function accepts a collection (for example, a tuple) and returns an enumerate object. The enumerate() function adds a counter as the enumerate object’s key. Syntax: enumerate(iterable, start) Parameter Values iterable: It is an iterable object. start: This …<\/p>\n
\n enumerate() Function in Python:<\/strong><\/p>\nenumerate(iterable, start)<\/pre>\n
Given List = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n
The given list after applying enumerate() function :\r\n[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]<\/pre>\n
Given List = ['good', 'morning', 'btechgeeks']\r\nGiven start value = 5<\/pre>\n
The given list after applying enumerate() function from the given start :\r\n[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]<\/pre>\n
enumerate() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without giving start value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['hello', 'this', 'is', 'btechgeeks']\r\n# Pass the given list to the enumerate() function as an argument that returns\r\n# an enumerate object. The enumerate() function adds a counter as the\r\n# enumerate object's key.\r\n# Store it in another variable.\r\nenumerte_lst = enumerate(gvn_lst)\r\nprint(\"The given list after applying enumerate() function :\")\r\n# Convert the above result to a list using the list() function and print the result.\r\nprint(list(enumerte_lst))\r\n<\/pre>\n
The given list after applying enumerate() function :\r\n[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]<\/pre>\n
2)With giving start value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks']\r\n# Give the number(start value) as static input and store it in another variable.\r\ngvn_strt = 5\r\n# Pass the given list, start value to the enumerate() function as the arguments\r\n# that returns an enumerate object from the given start value. The enumerate()\r\n# function adds a counter as the enumerate object's key.\r\n# Store it in another variable.\r\nenumerte_lst = enumerate(gvn_lst, gvn_strt)\r\nprint(\"The given list after applying enumerate() function from the given start :\")\r\n# Convert the above result to a list using the list() function and print the result.\r\nprint(list(enumerte_lst))\r\n<\/pre>\n
The given list after applying enumerate() function from the given start :\r\n[(5, 'good'), (6, 'morning'), (7, 'btechgeeks')]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),int(),split() and map() functions\r\n# and store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list to the enumerate() function as an argument that returns\r\n# an enumerate object. The enumerate() function adds a counter as the\r\n# enumerate object's key.\r\n# Store it in another variable.\r\nenumerte_lst = enumerate(gvn_lst)\r\nprint(\"The given list after applying enumerate() function :\")\r\n# Convert the above result to a list using the list() function and print the result.\r\nprint(list(enumerte_lst))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1000 2000 3000 4000\r\nThe given list after applying enumerate() function :\r\n[(0, 1000), (1, 2000), (2, 3000), (3, 4000)]<\/pre>\n
2)With giving start value<\/h5>\n
\n
# Give the list as user input using list(),int(),split() and map() functions\r\n# and store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the number(start value) as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_strt = int(input(\"Enter some random number = \"))\r\n# Pass the given list, start value to the enumerate() function as the arguments\r\n# that returns an enumerate object from the given start value. The enumerate()\r\n# function adds a counter as the enumerate object's key.\r\n# Store it in another variable.\r\nenumerte_lst = enumerate(gvn_lst, gvn_strt)\r\nprint(\"The given list after applying enumerate() function from the given start :\")\r\n# Convert the above result to a list using the list() function and print the result.\r\nprint(list(enumerte_lst))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 1 2 3 4 5\r\nEnter some random number = 100\r\nThe given list after applying enumerate() function from the given start :\r\n[(100, 1), (101, 2), (102, 3), (103, 4), (104, 5)]<\/pre>\n
\n