Showing posts with label enumerate in python. Show all posts
Showing posts with label enumerate in python. Show all posts

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.



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...