Python enumerate method


enumerate() is a built-in function of python. Enumerate means enumerate, enumerate in the dictionary for an iterable object (e.g. list, string).

enumerate forms it into an index sequence, which can be used to obtain both the index and the value, enumerate is mostly used to get counts in the for loop

Related course: Complete Python Programming Course & Exercises

enumerate

If, for a list, you want to iterate through both the index and the elements, you can first write this.

list1 = ["this", "yes", "one", "test"]
for i in range (len(list1)):
    print i ,list1[i]

The above methods are somewhat cumbersome and would be more straightforward and elegant with enumerate().

list1 = ["this", "yes", "one", "test"]
for index, item in enumerate(list1):
    print index, item

enumerate can also receive a second parameter that specifies the index start value, such as.

list1 = ["this", "yes", "one", "test"]
for index, item in enumerate(list1, 1):
    print index, item

python enumerate method

supplement

If you want to count the number of lines in a document, you can write it like this.

count = len(open(filepath, 'r').readlines())

This method is simple, but can be slow and doesn't even work when the file is larger.

It is possible to use enumerate().

count = 0
for index, line in enumerate(open(filepath,'r')): 
    count += 1

examples

Return enumeration type

The example below shows enumate examples.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
>>> s = "ABCD"
>>> list(enumerate(s))
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
>>> l = ["A", "B", "C", "D"]
>>> list(enumerate(l))
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
>>> t = ("A", "B", "C", "D")
>>> list(enumerate(t))
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
>>> d = {"A": 1, "B": 2}
>>> list(enumerate(d))
[(0, 'A'), (1, 'B')]
>>>

Example of enumate with for loop

The enumate function can be used with a for loop

>>>
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> count = 0
>>> for season in seasons:
...     print count,"=>", season
...     count += 1
...
0 => Spring
1 => Summer
2 => Fall
3 => Winter
>>>
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> for i in range(len(seasons)):
...     print i, "=>", seasons[i]
...
0 => Spring
1 => Summer
2 => Fall
3 => Winter
>>>

Example of iterating through enumerate

Some more examples of iterating through enumerate

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> for (index, season) in enumerate(seasons):
...     print index,"=>",season
...
0 => Spring
1 => Summer
2 => Fall
3 => Winter
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> for (index, season) in enumerate(seasons, start=1):
...     print index,"=>",season
...
1 => Spring
2 => Summer
3 => Fall
4 => Winter
>>>

Common practice

It's common to do this:

>>> count = len(open(r"D:\\1.txt").readlines())
>>> count
7

using enumerate

>>> for index,line in enumerate(open(r"D:\\1.txt"), start=1):
...     pass
...
>>> index
7