When this code is executed, it will print the number between 2 and 7 (2,3,4,5,6). So when you run the code as shown below, it prints the statement (guru99) that many times the number declared for our the variable in ( i in 123). First, values is initialized to be an empty list. It’s a built-in function, which means that it’s been available in every version of Python since it was added in Python 2.3, way back in 2003. Now imagine that, in addition to the value itself, you want to print the index of the item in the list to the screen on every iteration. You’re able to do this by using a Python concept called argument unpacking. Stuck at home? In the for loop, you assign the element from first to one, from second to two, and from third to three. Extract only some elements: slice; Counter (index): range() Elements and counter: enumerate() Multiple lists: zip() Multiple lists and counter: enumerate(), zip() Reverse order: reversed() check_whitespace() then makes several checks for out-of-place characters: When one of these items is present, check_whitespace() yields the current line number and a useful message for the user. What’s your #1 takeaway or favorite thing you learned? In Enumerate, you can specify the startIndex, i.e., the counter you want the values to start from. Just put it directly into a for loop… Get a short & sweet Python Trick delivered to your inbox every couple of days. On each iteration of the loop, value is set to the next item from values. Let’s see what to do if we want to enumerate a python list. enumerate() IN PYTHON is a built-in function used for assigning an index to each item of the iterable object. In our example, we have declared value 10-20, but between these numbers we only want those number that are NOT divisible by 5 or in other words which don't give zero when divided by 5. Looping in python is a little bit confusing because the for loop in python is not for loop, it is actually a for each loop. Above codes are Python 3 examples, If you want to run in Python 2 please consider following code. Complete this form and click the button below to gain instant access: "CPython Internals: Your Guide to the Python 3 Interpreter" – Free Sample Chapter (PDF). To unpack the nested structure, you need to add parentheses to capture the elements from the nested tuple of elements from zip(). Python eases the programmers’ task by providing a built-in function enumerate() for this task. This lets you get the index of an element while iterating over a list. For instance, you can unpack a tuple of two elements into two variables: First, you create a tuple with two elements, 10 and "a". How are you going to put your newfound skills to use? Keep in mind that enumerate() increments the count by one on every iteration. In the last few sections, you saw examples of when and how to use enumerate() to your advantage. Just like while loop, "For Loop" is also used to repeat the program. While Loop is used to repeat a block of code. Syntax for iterating_var in sequence: statements(s) If a sequence contains an expression list, it is evaluated first. The result will be [2, 4, 6, 8, 10]: As expected, even_items() returns the even-indexed items from seq. 2. The enumerate Function in Python – Key Takeaways: enumerate is a built-in function of Python which is used to loop over an iterable with an automatic running index that has been generated by a counter variable. Finally, calling next() one more time raises StopIteration since there are no more values to be returned from enum_instance. A Python variable is a reserved memory location to store values. Then range() creates an iterator running from the default starting value of 0 until it reaches len(values) minus one. In this Python tutorial we will concentrate on Python enumerate() function and it's usage with some simple and real time examples.. Python enumerate() function syntax. Flow of control enters into while Loop. Finally, you increment n to get ready for the next iteration. When code is executed the output of the enumerate function returns the months name with an index number like (0-Jan), (1- Feb), (2- March), etc. """, ['b', 'd', 'f', 'h', 'j', 'l', 'n', 'p', 'r', 't', 'v', 'x', 'z'], , [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')], [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')], Get a sample chapter from CPython Internals: Your Guide to the Python 3 Interpreter, Any spaces or tabs at the end of the line, Accept an iterable and a starting count value as arguments, Send back a tuple with the current count value and the associated item from the iterable. Basics of the for loop in Python. Python 3 - for Loop Statements. It's worth noting that this is the fastest and most efficient method for acquiring the index in a for loop. This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc. In this code, number 7 is not considered inside the range. Code Line 3: We store the months ("Jan, Feb , Mar,April,May,June") in variable Months, Code Line 4: We iterate the for loop over each value in Months. You also have to change the loop variable a little bit, as shown in this example: When you use enumerate(), the function gives you back two loop variables: Just like with a normal for loop, the loop variables can be named whatever you want them to be named. Related: for loop in Python (with range, enumerate, zip, etc.) """, """Return items from ``iterable`` when their index is even. enumerate() Parameters. There’s no ending index after the first colon, so Python goes to the end of the string. Not familiar with python for in, while and enumerate loop and how to get the list index of elements? By definition, iterables support the iterator protocol, which specifies how object members are returned when an object is used in an iterator. The team members who worked on this tutorial are: Master Real-World Python Skills With Unlimited Access to Real Python. You can combine zip() and enumerate() by using nested argument unpacking: In the for loop in this example, you nest zip() inside enumerate(). In this case, index becomes your loop variable. Enjoy free courses, on us →, by Bryan Weber Sometimes, when we're looping over a container in a for loop, we want access to the index (the current position in the list) of the current item being processed.One way to handle this would be: For instance, you might need to return items from an iterable, but only if they have an even index. Copy/paste the following code and try it out yourself. In this tutorial, we will learn how to loop in steps, through a collection like list, tuple, etc. This is what you did earlier in this tutorial by using two loop variables. Python assigns the value it retrieves from the iterable to the loop variable. In this article, I’ll show you when you can replace range with enumerate or zip . In other... What is Python strip()? enumerate(iterable, start=0) The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary. Loops can execute a block of code number of times until a certain condition is met. Share This enumerate object can then be used directly in for loops or be converted into a … Continue statement can be used in for loop when you want to fetch a specific value from the list. This continues till x becomes 4, and the while condition becomes false. Continue function, as the name indicates, will terminate the current iteration of the for loop BUT will continue execution of the remaining iterations. This object can be used in a for loop to convert it into a list by using list() method. One method uses itertools.count(), which returns consecutive integers by default, starting at zero. This way, you don’t need to remember to update the index: In this example, len(values) returns the length of values, which is 3. A for loop in Python uses collection-based iteration. Convert a tuple into an enumerate object: Rationale. Leave a comment below and let us know. In Python, "for loops" are called iterators. In this step, we will see how "for loops" can also be used for other things besides numbers. Email. You should use enumerate() anytime you need to use the count and an item in a loop. Then you use Python’s built-in next() to get the next value from enum_instance. Next Page . In other words, when you want to retrieve the first element of a list, you use index 0: You can see in this example that accessing values with the index 0 gives the first element, a. Python’s enumerate() lets you write Pythonic for loops when you need a count and the value from an iterable. basics Argument unpacking is the idea that a tuple can be split into several variables depending on the length of the sequence. 2. The last step in the loop is to update the number stored in index by one. You can make the code more Pythonic by using a list comprehension to do the same thing in one line without initializing the empty list: In this example code, even_items() uses a list comprehension rather than a for loop to extract every item from the list whose index is an even number. Conclusion: Among all the looping techniques, simple for loop iteration and looping over iterators works best, while comparing all the techniques, using map with lambda over set or iterator of set works best giving a performance of a million set iterations under 10 … range() function allows to increment the “loop index” in required amount of steps. C++ is widely used in general-purpose programming languages. You’ll often see reST-formatted strings included as docstrings in Python classes and functions. Rather than creating and incrementing a variable yourself, you can use Python’s enumerate() to get a counter and the value from the iterable at the same time! So except number 10,15 & 20 the "for loop" will not continue and print out those number as output. Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. enumerate(iterable, start=0) iterable - a sequence, an iterator, or objects that supports iteration start – is the position in the iterator from where the counting starts. We will go through each of them and their variations with examples. Code Line 11 declare the condition for breakpoint at x==15, Code Line 12 checks and repeats the steps until it reaches number 15, Code line 10 declare the variable x for range (10, 20), Code line 12 declare the condition for x divided by 5=0 continue, Code Line 3 declares the list of months [ Jan, Feb,…Jun], Code Line 4 declares variable i and m for For Loop, Code Line 5 will print the result and again enter the For Loop for the rest of the months to enumerate. For Loops can also be used for a set of other things and not just number. Another time you might have seen argument unpacking with a for loop is with the built-in zip(), which allows you to iterate through two or more sequences at the same time. Instead of putting the iterable directly after in in the for loop, you put it inside the parentheses of enumerate(). 0 is where the counter starts by default, but you can set it to any integer. Enumerate() function is a built-in function available with python. The current value of x is 0. Python functions, whether generators or regular functions, can’t be accessed by the square bracket indexing. By default, the starting value is 0 because Python sequence types are indexed starting with zero. Next, you show that calling my_enumerate() with seasons as the sequence creates a generator object. It allows us to loop over something and have an automatic counter. To iterate through an iterable in steps, using for loop, you can use range() function. You can use for loop for even repeating the same statement over and again. Example: To repeat same statement number of times, we have declared the number in variable i (i in 123). This returns the line number, abbreviated as lno, and the line. Python’s enumerate() has one additional argument that you can use to control the starting value of the count. The first user is your testing user, so you want to print extra diagnostic information about that user. Python For Loops. Traditionally, this has been done by using a for loop, utilizing range and len to create a list of indices. However, there are many times when you might not want the count from enumerate() to start at 0. You can change the previous example to use itertools.count(): Using itertools.count() in this example allows you to use a single zip() call to generate the count as well as the loop variables without nested argument unpacking. Related: enumerate() in Python: Get the element and index from a list Here in the example we have printed out word "guru99" three times. Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The first element of the tuple is the count, and the second element is the value from the sequence that you passed: In this example, you create a list called values with two elements, "a" and "b". A common bug occurs when you forget to update index on each iteration: In this example, index stays at 0 on every iteration because there’s no code to update its value at the end of the loop. The items of the list then are accessed using [] notation, like so: In Python, enumerate() and zip() are useful when iterating elements such as lists in a for loop. Sometimes you might need to perform an action on only the very first iteration of a loop, as in this example: In this example, you use a list as a mock database of users. This is one reason that this loop isn’t considered Pythonic. Unsubscribe any time. enumerate() is an iterator, so attempting to access its values by index raises a TypeError. For loop is used to iterate over elements of a sequence. You can do this by using enumerate(): even_items() takes one argument, called iterable, that should be some type of object that Python can loop over. Fortunately, Python’s enumerate() lets you avoid all these problems. Flow of control goes back to line 7. We will see thin in next section. You can iterate over the index and value of an item in a list by using a basic for loop L = ['apples', 'bananas', 'oranges'] for idx, val in enumerate(L): print("index is %d and value is %s" % (idx, val)) The first value that enum_instance returns is a tuple with the count 0 and the first element from values, which is "a". Python strings are sequences, which can be used in loops as well as in integer indexing and slicing. When you use enumerate() in a for loop, you tell Python to use two variables, one for the count and one for the value itself. You use count and value in this example, but they could be named i and v or any other valid Python names. You can see that the result is the same as the previous two examples. Python Tuples. ... Python enumerate() Function Built-in Functions. This isn’t the most efficient way to get the even numbers when you’re working with integers. Python has two commonly used types of iterables: Any iterable can be used in a for loop, but only sequences can be accessed by integer indices. Let see another example for For Loop to repeat the same statement over and again. Tweet Finally, you print index and value. The big advantage of enumerate() is that it returns a tuple with the counter and value, so you don’t have to increment the counter yourself. If it is, then you append the item to values. enumerate() method takes two parameters: iterable - a sequence, an iterator, or objects that supports iteration; start (optional) - enumerate() starts counting from this number. It’s a common use case to iterate over a list together with its index. No spam ever. enumerate() is a built-in Python function which is very useful when we want to access both the values and the indices of a list. The unification came in the form of establishing a common iterable interface for mappings, sequences, and file objects. The count variable lno has 1 added to it so that it returns the counting line number rather than a zero-based index. "For Loop" depends on the elements it has to iterate. Since start isn’t used, lno is a zero-based counter of the lines in the file. One way to approach this task is to create a variable to store the index and update it on each iteration: In this example, index is an integer that keeps track of how far into the list you are. To get a better sense of how enumerate() works, you can implement your own version with Python. Their usage is fairly common in programming. Enumerate is a built-in function of Python. When a user of rstlint.py reads the message, they’ll know which line to go to and what to fix. Finally, you return values. However, now that you’ve verified that even_items() works properly, you can get the even-indexed letters of the ASCII alphabet: alphabet is a string that has all twenty-six lowercase letters of the ASCII alphabet. You can also combine mathematical operations with conditions for the count or index. Python Lists Access List Items Change List Items Add List Items Remove List Items Loop Lists List Comprehension Sort Lists Copy Lists Join Lists List Methods List Exercises. This means that Python assigns the next item from an iterable to the loop variable on every iteration, like in this example: In this example, values is a list with three strings, "a", "b", and "c". This example is slightly modified from rstlint.py. Using enumerate() also makes the code cleaner , since you have to write fewer lines. The second method to iterate through the list in python is using the while loop. Suppose, we want to do numbering for our month ( Jan, Feb, Marc, ….June), so we declare the variable i that enumerate the numbers while m will print the number of month in list. Bryan is a mechanical engineering professor and a core developer of Cantera, the open-source platform for thermodynamics, chemical kinetics, and transport. let’s use the enumerate() method to get the element at the 3rd index. Iterate Through List in Python Using While Loop. So in the case of strings, you can use square brackets to achieve the same functionality as even_items() more efficiently: Using string slicing here, you give the starting index 1, which corresponds to the second element. Join us and get access to hundreds of tutorials, hands-on video courses, and a community of expert Pythonistas: Master Real-World Python SkillsWith Unlimited Access to Real Python. The point is to show a real-world use of enumerate(): check_whitespace() takes one argument, lines, which is the lines of the file that should be evaluated. Yet most of the newcomers and even some advanced programmers are unaware of it. Calling next() again on enum_instance yields another tuple, this time with the count 1 and the second element from values, "b". Python’s built-in enumerate function allows us to loop over a list and retrieve both the index and the value of each item in the list: 1 2 3 presidents = [ "Washington" , "Adams" , "Jefferson" , "Madison" , "Monroe" , "Adams" , "Jackson" ] for num , name in enumerate ( presidents , start = 1 ): print ( "President {}: {}" . While loops are executed based on whether the conditional statement is true or false. Enumerate function is used for the numbering or indexing the members in the list. For Loop iterates with number declared in the range. Code Line 9: x is incremented by 1. The condition is true, and again the while loop is executed. Nov 18, 2020 Round() Round() is a built-in function available with python. If start is omitted, 0 is taken as start. Your version of enumerate() has two requirements. Next, you print value onto the screen. In addition, it can also take in an optional argument, start, which specifies the number we want the count to start at (the default is 0). In this tutorial, we shall go through some of the processes to loop through items in a list, with well detailed Python … You can verify that even_items() works as expected by getting the even-indexed items from a range of integers from 1 to 10. The built-in enumerate() function allows you to loop over a list of items while keeping track of the index value in a separate variable. Then you print the three values. """Check for whitespace and line length issues. In the next few sections, you’ll see some uses of enumerate(). In other programming languages (C), you often use a for loop to get the index, where you use the length of the array and then get the index using that. There are other ways to emulate the behavior of enumerate() combined with zip(). The for statement in Python has the ability to iterate over the items of any sequence, such as a list or a string. Example. How and why you should use the built-in enumerate function in Python to write cleaner and more Pythonic loops. Python List – Loop through items. best-practices. Note: reST, also known as reStructured Text, is a standard format for text files that Python uses for documentation. This is similar to the previous bug of forgetting to update the index. Enumerate() function adds a counter to each item of the iterable object and returns an enumerate object. In both cases, you end up with a list of tuples in which the first element of each tuple is the count and the second element is the value from seasons. Something like a key, value pair of a dictionary. This powerful Python feature will be discussed further a little later in this article. Enumerate can be used to loop over a list, tuple, dictionary, and string. This is because you use the yield keyword to send values back to the caller. The advantage of collection-based iteration is that it helps avoid the off-by-one error that is common in other programming languages. It adds a loop on the iterable objects while keeping track of the current item and returns the object in an enumerable form. If an iterable returns a tuple, then you can use argument unpacking to assign the elements of the tuple to multiple variables. In Python, a for loop is usually written as a loop over an iterable object. Now the value of x is 1 which is less than 4. With enumerate(), you don’t need to remember to access the item from the iterable, and you don’t need to remember to advance the index at the end of the loop. Related Tutorial Categories: In while loop way of iterating the list, we will follow a similar approach as we observed in our first way, i.e., for-loop method. You can see one example in a script that reads reST files and tells the user when there are formatting problems. Its usefulness can not be summarized in a single line. So, in our range (10,11, 12….19,20) only 3 numbers falls (10,15,20) that are divisible by 5 and rest are not. Using enumerate() function: The enumerate() function of Python allows you to loop over the iterable and provides you with an automatic counter which gets incremented automatically for each iteration. In the previous section, you saw how to use enumerate() with start to create a natural counting number to print for a user. Python 2.2 introduced the concept of an iterable interface as proposed in PEP 234.The iter() factory function was provided as common calling convention and deep changes were made to use iterators as a unifying theme throughout Python. Technical Detail: According to the Python documentation, an iterable is any object that can return its members one at a time. In the loop, you set value equal to the item in values at the current value of index. Although you can implement an equivalent function for enumerate() in only a few lines of Python code, the actual code for enumerate() is written in C. This means that it’s super fast and efficient. However, this only slightly limits your flexibility. Calling even_items() and passing alphabet returns a list of alternating letters from the alphabet. Then you create a for loop over iterable with enumerate() and set start=1. You can use enumerate() in a loop in almost the same way that you use the original iterable object. Better Python for Loops Using enumerate November 19, 2020. enumerate() is also used like this within the Python codebase. Python For Loop Increment in Steps. Using Python’s enumerate() You can use enumerate() in a loop in almost the same way that you use the original iterable object. You now have the superpower of simplifying your loops and making your Python code stylish! For loops are called iterators, it iterates the element based on the condition set, Python For loops can also be used for a set of various other things (specifying the collection of elements we want to loop over), Breakpoint is used in For Loop to break or terminate the program at any particular point, Continue statement will continue to print out the statement, and prints out the result as per the condition set, Enumerate function in "for loop" returns the member of the collection that we are looking at with the index number. Condition is true. Technical Details: The use of the two loop variables, count and value, separated by a comma is an example of argument unpacking. The Python range function is very powerful, but it can often be replaced with other built-in functions that make your loops easier to write and read. For each elem in sequence, you yield control back to the calling location and send back the current values of n and elem. Index with enumerate ( ) and set start=1 better sense of how enumerate ( ) combined with len ( in... Protocol, which are each assigned one of the lines in the for loop you. Loop through the list in Python convert it into a for loop, you increment n get! Use range ( ) function allows to increment the “ loop index ” required. After the first colon, so Python goes to the Python documentation an! Programmers are unaware of it this step, we will see how `` for loops '' are called sequences Python! Continue and print out those number as output, utilizing range and len to create an index each. Home.... what is Python strip ( ) in a loop in steps, using for loop,! Rest-Formatted strings included as docstrings in Python 2 please consider following code and try out. And passing alphabet returns a tuple can be used to repeat the program enumerate function is used in an form! And most efficient method for acquiring the index of an item in values at the index! Statement can be used to iterate through a dictionary simplest way to get the elements it has to through! Or zip quality standards print out those number as an output for the numbering or indexing members. Loop variables: for loop when you ’ ll show you when you need a and. Of times until a certain condition is met of days on the of... Items can be used in some real-world code, number 7 is not considered inside the parentheses of enumerate ). ) also makes the code block multiple times until a certain condition is met first... Append the item in values at the current item and returns the object in iterator! T need a count and value in this case, index becomes loop... Retrieves from the default starting value of start and run a for iterates. Track of the current value of 0 until it reaches len ( ) used in an iterator: reST also. S no ending index after the first user is your testing user, so you want count. For instance, you can specify the startIndex, i.e., the variable! Explained ( with range, enumerate ( ) in action here: first, you saw examples of and. User, so you want to repeat a block of code number of times we! Built-In function enumerate ( ) method adds a counter to each item the. Increment the “ loop index ” in required amount of steps a short & sweet Python Trick to... Called.__iter__ ( ) lets you get the next value from an iterable returns a tuple with elements... Step in the next item from values to iterate through a collection like list, tuple, dictionary, transport. Each assigned one of the enumerate python for loop or indexing the members in the example we have the..., i ’ ll know which line to go to and what to fix for next... Enjoy free courses, on us →, by Bryan Weber Nov 18 2020. It allows us to loop over a list of alternating letters from the tuple multiple! It yields a tuple can be split into several variables depending on the length of the values from the to! Might not want the count and value in this article a standard format Text... Note: reST, also known as reStructured Text, is a standard integer! You did earlier in this article, i ’ ll show you when you have to write cleaner more. Smart enough to know that a_dict is a standard format for Text files that Python will every! That changes on each iteration dividing index by 2 is zero with number in! ( i in 123 ), using for loop, you put it directly into a loop. This powerful Python feature will be discussed further a little later in this code is executed tuple,,! Object is used to repeat `` n '' number of time the items of the iterable objects while keeping enumerate python for loop. You call enumerate ( ), and file objects a team of developers that! You did earlier in this example is also used like this within the CPython code repository please consider code! Of when and how to loop in almost the same statement number of times a... To approach this problem is to update the number stored in index one..., values is initialized to be the value of index amount of.! In variable i ( i in 123 ) with Python since start enumerate python for loop. Length issues used when you call enumerate ( ) for Text files that Python will take other! Is true or false lets you get the index integers by default, the open-source platform for,... Worked on this tutorial are: Master real-world Python Skills with Unlimited access to items... It returns the line number rather than a zero-based counter of the current item and returns it in a that! Run in Python of check_whitespace ( ) automatically, and string into first_elem and second_elem, can... Returns an enumerate object for the user part of built-in functions available in the next from! Which is less than 4 declared the number in variable i ( i in 123 ) ” required! Often used when you print enum_instance, you can see my_enumerate (,. Ll see some uses of enumerate ( ) to start from unpacking to assign the element at the beginning each! An expression list, tuple, then you unpack that tuple into first_elem and second_elem, specifies. Emulate the behavior of enumerate ( ) to your inbox every couple of days equal to the end of tuple... The programmers ’ task by providing a built-in function available with Python sequence creates a generator object languages... Combined with len ( ) lets you write Pythonic for loops when you forget update! To put your newfound Skills to use enumerate ( ) with seasons as the sequence because..., like so: enumerate is a built-in function used for a set of other things and just... Each assigned one of these features, which people often forget about is the simplest to! Is, then you can use range ( ) and assign the return value to.! Numbering or indexing the members in the next few sections, you can replace with. Counting line number rather than a zero-based index or index with conditions the... The example we have printed out word `` guru99 '' three times index by 2 is zero s built-in (... Each item of the list more values to be an empty list very powerful technique value 0... Guide to getting you started on this tutorial, we will learn how to over! Changes on each loop iteration your testing user, so you want to in... Element at the current value of index useful when iterating elements such as lists in a form of enumerate )... To one, from second to two, and this allowed you to iterate the value... Value equal to the item in a for loop over something and have an automatic counter element while iterating a! A block of code number of times until a certain condition is met equal to calling! Statement in Python is created by a 2 so that it ’ s (. Set it to any integer you put it inside the parentheses of enumerate ( to! Through each of them and their variations with examples ) the enumerate ( ) has requirements. Makes the code block multiple times until a certain condition is true and! Able to do this by using a for loop '' is also used this. Directly into a list by using a for loop taken as start value! Which you want to have a piece of code which you want to the! The alphabet in values at the 3rd index reST-formatted strings included as docstrings in Python ( i in 123.! A dictionary in Python, `` '' '' return items from `` ``... To your advantage also saw enumerate ( ) in Python, a for over. S use the count variable lno has 1 added to it so that meets... The condition is met technical Detail: According to the Python documentation, iterable. Be split into several variables depending on the elements of multiple lists together with zip ( ) is a format... You going to put your newfound Skills to use range ( ) for this task programming languages when object... Integer, you set value equal to the item itself utilizing range and len to an! Counting number as output first user is your testing user, so you want the count lno... Built-In function available with Python items in the for loop when you can see my_enumerate ( has! Counter starts by default, starting at zero example for for loop over an iterable in steps, using loop. Your testing user, so attempting to access its values by index a. For even repeating the same statement over and again the Python documentation, an iterable.! Calling next ( ) in Python be discussed further a little later this... An element while iterating over a list enumerate python for loop tuple, then you unpack that tuple into first_elem and second_elem which. Keep in mind that enumerate ( ) in Python, a for loop… Python for loops using (... Elements it has to allow access to its items using integer indices establishing a common use case to iterate the! ( ) are useful when iterating elements such as lists in a for loop it implements.__iter__ ( used...