Home Python How to use Python enumerate () function?

How to use Python enumerate () function?

0
How to use Python enumerate () function?

Many times when managing iterators, we likewise get a need to keep a count of iterations.

Python facilitates the developers’ errand by giving an inherent capacity list() for this undertaking.

List() strategy adds a counter to an iterable and returns it in a type of identifying the object.

This specify item would then be able to be utilized directly in for circles or be changed over into a rundown of tuples utilizing the list() technique.

Contents

The Python Enumerate() function

Python Enumerate() is a built-in work accessible with the Python library.

It takes the given contribution as an assortment or tuples and returns it as an enumerated object.

The Python Enumerate() order adds a counter to everything of the iterable item and returns a specified object as a yield string.

The Parameters:

  1. Iterable: an object that can be circled.
  2. StartIndex: (discretionary) The check will begin with the worth given in the startIndex for the principal thing on the loop and up and increase it for the next item till it arrives at the end of the loop.
  3. Return Value: It will return an iterable object, with count value to every one of the things to the iterator object given as info

The enumerate function of Python:

  • The enumerate() work in Python is ordinarily utilized rather than the for circle.
  • That is on the grounds that identify() can repeat over the file of a thing, just as the actual thing.
  • Utilizing specify() likewise makes the code cleaner since you need to compose fewer lines.
  • In Enumeration in Python, you can indicate the startIndex, i.e., the counter you need the qualities to begin from.
  • The list can be utilized to circle over a rundown, tuple, word reference, and string.
  • Enumerate is exceptionally useful when you need the record and the worth when circling over the list, tuple, word reference, and so on.
  • Enumerate accompanies a programmed counter/record to every one of the things present in the list.
  • The primary record worth will begin from 0.
  • You can likewise indicate the start index by utilizing the discretionary boundary startIndex in identify.
  • On the off chance that you pass a string to count(), the yield will show you the record and an incentive for each character of the string to the enumerate function of python.

Explanation:

Enumerate in Python

Example:

Enumerate strategy accompanies a programmed counter/list to every one of the things present in the Enumerate list in Python.

The first index worth will begin from 0.

You can likewise indicate the start index by utilizing the discretionary boundary startIndex in the list.

There is no startIndex utilized subsequently the list for the first item will begin from 0.

The output from specify will be in the accompanying way:

(0, item_1), (1, item_2), (2, item_3), … (n, item_n)

File: python_enumerate.py


Mylist = [‘A’, ‘B’ ,’C’, ‘D’]

E_list = enumerate(mylist)

Print(list(e_list))

Output:

[(0, ‘A’), (1, ‘B’), (2, ‘C’), (3, ‘D’)]

Using Enumerate ()on a list with starIndex

The start index is given as 2.

The file of the first item will begin from the given start index.

Example:

In the model beneath, my list is the rundown given to the list.

The rundown() work is utilized to show the count yield.


Mylist = [‘A’, ‘B’ ,’C’, ‘D’]

E_list = enumerate(mylist,2)

Print(list(e_list))

Output:

[(2, ‘A’), (3, ‘B’), (4, ‘C’), (5, ‘D’)]

Looping over an Enumerate Object

The examples show listing over an object with and without startIndex.

The first for-loop doesn’t have startIndex, so the record begins from 0.

The second for-loop has startIndex as 10, so the file is beginning from 10.

Example:


Mylist = [‘A’, ‘B’,’ C’, ‘D’]

for I in enumerate(mylist):

Print(i)

Print(“\n”)

Print(“Using startIndex as 10”)

For I in enumerate(mylist, 10):

Print(i)

Print(“\n”)

Output:

(0, ‘A’)

(1, ‘B’)

(2, ‘C’)

(3, ‘D’)

Utilizing startIndex as 10

(10, ‘A’)

(11, ‘B’)

(12, ‘C’)

(13, ‘D’)

Enumerating a Tuple

You can utilize a tuple inside a enumerate.

You can likewise utilize a startIndex, and the way into everything will begin from the startIndexgiven.

As a matter of course, the startIndex is 0 There, thus you consider the key to be 0 for things An and 1 for B, etc…

Example:


My_tuple = (“A”, “B”, “C”, “D”, “E”)

For I in enumerate(my_tuple):

Print(i)

Output:

(0, ‘A’)

(1, ‘B’)

(2, ‘C’)

(3, ‘D’)

(4, ‘E’)

Enumerating a String

In Python, the string is an array, and subsequently, you can loop over it.

In the event that you pass a string to identify(), the output will show you the list and an incentive for each character of the string

Example:


My_str = “Wholeblogs “

For I in enumerate(my_str):

Print(i)

Output:

(0, ‘W’)

(1, ‘h’)

(2, ‘o’)

(3, ‘l’)

(4, ‘e’)

(5, ‘b’)

(6, ‘l’)

(7, ‘o’)

(8, ‘g’)

(9, ‘s’)

Read More: How to Convert a Python String to Integers?

LEAVE A REPLY

Please enter your comment!
Please enter your name here