{"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>
\n enumerate() Function in Python:<\/strong><\/p>\n

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

enumerate(iterable, start)<\/pre>\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

Given List = ['hello', 'this', 'is', 'btechgeeks']<\/pre>\n

Output:<\/strong><\/p>\n

The given list after applying enumerate() function :\r\n[(0, 'hello'), (1, 'this'), (2, 'is'), (3, 'btechgeeks')]<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given List = ['good', 'morning', 'btechgeeks']\r\nGiven start value = 5<\/pre>\n

Output:<\/strong><\/p>\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