For xy in enumerate python
enumerate- Iterate over indices and items of a list¶The Python Cookbook (Recipe 4.4) describes how to iterate over items and indices in a list using Show
alist = ['a1', 'a2', 'a3'] for i, a in enumerate(alist): print i, a Results: 0 a1 1 a2 2 a3 zip- Iterate over two lists in parallel¶I previously wrote about using alist = ['a1', 'a2', 'a3'] blist = ['b1', 'b2', 'b3'] for a, b in zip(alist, blist): print a, b Results: a1 b1 a2 b2 a3 b3 enumerate with zip¶Here is how to iterate over two lists and their indices using enumerate together with zip: alist = ['a1', 'a2', 'a3'] blist = ['b1', 'b2', 'b3'] for i, (a, b) in enumerate(zip(alist, blist)): print i, a, b Results: 0 a1 b1 1 a2 b2 2 a3 b3 How do I get the index of an element while I am iterating over a list? If you are coming from other programming languages (like C), most likely you are used to the idea of iterating over the length of an array by an index and then using this index to get the value at that location. 😓😓😓 Python gives you the luxury of iterating directly over the
values of the list which is most of the time what you need. However, there are times when you actually need the index of the item as well. Python has a built-in function called enumerate that allows you to do just that. In this article, I will show you how to iterate over different types of python objects and get back both the index and the value of each item. Jump directly to a specific section:
Enumerate a ListLet’s see what to do if we want to enumerate a python list. You can iterate over the index and value of an item in a list by using a basic for loop
The code above will have this output:
How easy was that! Now let’s see how to enumerate a tuple. Enumerate a TupleEnumerating a tuple isn’t at all different from enumerating a list.
And as you expect, the output will be:
Now that you know how to enumerate a list and a tuple, how can you enumerate a list of tuples? 🤔🤔🤔 Enumerate a List of Tuples (The Neat Way)Say you have a list of tuples where each tuple is a name-age pair.
Of course one way to enumerate the list is like this:
The above code will definitely work and it will print out this output.
However, a cleaner way to achieve this is through tuple unpacking With tuple unpacking, we can do something like this
Enumerate a StringSo what happens when you use the enumerate function on a string object? An item in a string is a single character. So if you enumerate over a string, you will get back the index and value of each character in the string. Let’s take an example:
And the output will be:
Enumerate with a Different Starting IndexSo as you know, indices in python start at 0. This means that when you use the enumerate function, the index returned for the first item will be 0. However, in some cases, you want the loop counter to start at a different number. enumerate allows you to do that through an optional start parameter. For example, let’s say we want to enumerate over a list starting at 1. The code will look like this:
The above code will result in this output.
Needless to say, you can also start with a negative number. Why It doesn’t Make Sense to Enumerate Dictionaries and SetsSo does it make sense to use the enumerate function for dictionaries and sets? Absolutely not! Think about it, the only reason you would use enumerate is when you actually care about the index of the item. Dictionaries and Sets are not sequences. Their items do not have an index, and they don’t, by definition, need one. If you want to iterate over the keys and values of a dictionary instead (a very common operation), then you can do that using the following code:
And if you want to iterate over a set, then just use a regular for loop.
Advanced: Enumerate Deep DiveIn Python, the enumerate function returns a Python object of type enumerate Yes, there is an enumerate built-in function and an enumerate object 🙂
Now let’s go to Github and check how the enumerate object is implemented. As you can see, the enumerate object stores an index en_index, an iterator en_sit, and a result tuple en_result en_sit is actually the input parameter that we passed to the enumerate function. It must be an iterable object. At a given index, the result is a tuple of two elements. The first element is the index and the second one is the item in en_sit with that index. enumerate objects themselves are also iterables with each item being the mentioned result tuple.
That’s why when we iterate over the enumerate object with a for loop like this:
We are effectively unpacking these tuples to an index and a value. But there is nothing that prevents you from doing this (but don’t do it :))
Finally, have fun enumerating 🙂 Learning Python?Check out the Courses section! Featured Posts
Are you Beginning your Programming Career?I provide my best content for beginners in the newsletter.
And so much more… Subscribe now. It’s Free.How do you enumerate a list in Python?Enumerate() in Python
Enumerate() method adds a counter to an iterable and returns it in a form of enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() method. Parameters: Iterable: any object that supports iteration.
What does enumerate () do in Python?Python enumerate() Function
The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.
Can zip function be used with enumerate?If you want to get the elements of multiple lists and indexes, you can use enumerate() and zip() together. In this case, you need to enclose the elements of zip() in parentheses, like for i, (a, b, ...) in enumerate(zip( ... )) . You can also receive the elements of zip() as a tuple.
What does enumerate do in for loop?Using the enumerate() Function
enumerate() allows us to iterate through a sequence but it keeps track of both the index and the element. The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary.
|