Wednesday, January 30, 2019

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element. Suppose you have a text file or a list or element which contains multiple elements , the enumerate() function can be used to numbering them. As in the example below -I have a file which has multiple lines of text. I can number each line by using this simple technique -Here we open a file and read the lines but while reading we can count the line no by using enumerate.

                                               abc=open('abc.txt')
                                               for count,text in enumerate(abc.readlines()):
                                                   print (count,text)

Also, we can enumerate lists and dictionary with this function in the same way.



No comments:

Post a Comment

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...