Skip to main content

Learning the Enumerate()


After, the list lets learn about the intriguing enumerate function in python. Python is the favorite programming language of many developers; contrary to popular belief, it isn’t merely a “technology for newbies” its power and functionality make it an excellent tool for a plethora of tasks. Python is renowned for its collection of libraries, modules, and functions that it comes packaged with and enumerate() is something we should learn in detail.


          So, what is enumerate function?

          Python's enumerate function is tool that allows us to create a counter alongside values we're iterating over: as part of a loop, for example.

Enumerate takes an iterable type as its first positional argument, such as a list, and returns an enumerate object containing a number of tuples: one for each item in the iterable. Each tuple contains an integer counter, and a value from the iterable.

If we ever have to iterate through a list while keeping track of the index of each element, enumerate will come in handy.

Let’s see through an example:

words = ['all', 'your', 'base']
for idx, word in enumerate(words):
    print(idx, word)
print(list(enumerate(words)))# Output:
# 0 all
# 1 your
# 2 base
# [(0, 'all'), (1, 'your'), (2, 'base')]

 Enumerate returns an iterator object instead of a list. The iterator serves to save memory, especially when your input list is large.

So, this is how enumerate function comes handy when we need to keep the count of iteration when dealing with lot of iterations.
You can learn more about this enumerate from here:



Comments

Post a Comment

Popular posts from this blog

Project Documentation

Course Project Title: HANGMAN GAME in Python Description: An interactive game of Hangman developed using python(an object programming language).The project consists of a number of functions used in the game by making use of inbuilt Python libraries.The game offers the user to choose number of attempts and also shows a graphical output. Link for project report: here Final Code File :  Python Code Topic 1: Hangman graphic function Presentor's info:  Name: Siddhesh Ramajgol Div: TY-L Roll no: 57 GR.no : 1710081 Video Link:   Hangman Graphic Function Topic 2: Get no.of attempts and word length Presentor's info:  Name:  Kartik Mehetre Div: TY-L Roll no: 47 GR.no :  1710382 Video Link:  No.of attempts and word length Topic 3: Get next letter and display words Presentor's info:  Name: Shreya Uttarwar Div: TY-L Roll no: 69 GR.no :  1710360 Video Link:  Get next letter and display ...

The output!

So here we arrive at the OUTPUT ! When you run the code in any Python IDE you will get results as follows: We are planning to put a video explaining each part of the source code in the next and final post for this blog. So, do visit again! Happy Learning!                                                             -Kartik Mehetre

Further into the lists..

So, let's continue our learnings about the lists in python!      Why and how  to use list comprehension ? List is one of the most useful collection data type in python.It is a collection which is ordered and changeable.It Allows duplicate members.   Many of us would have thought of what if we want to manipulate each and every  list element? Suppose you are given a list of numbers and you want to add 1 to each element in your list: my_list = [1, 3, 3, 7] my_list_plus_one = my_list + 1  and if you have thought of some code like above then its not gonna  work. You would get this error:   TypeError : can only concatenate list (not "int") to list.  Python thinks that you want to add a list and an integer together, which doesn’t make any sense. So how do we manipulate the list? The answer is using list comprehension syntax. Example: my_list_plus_one = [num + 1 for num in my_list] print(my_list_plus_one)  # Output: [2, 4, 4, 8] Essentially...