In Show
Can some one tell how to do this in asked Aug 8, 2018 at 13:28
rashokrashok 12k13 gold badges85 silver badges97 bronze badges 3 With
answered Aug 8, 2018 at 13:36
rashokrashok 12k13 gold badges85 silver badges97 bronze badges 2 If the first answer does not suffice; to account for lists of different sizes, a possible option would be:
Or if one wants to cycle around shorter list:
answered Aug 8, 2018 at 13:43
sdgaw erzswersdgaw erzswer 1,9681 gold badge23 silver badges45 bronze badges You can do this in Python by using the syntax
Here is an example:
answered Aug 8, 2018 at 13:42
Laurent H.Laurent H. 5,9761 gold badge17 silver badges37 bronze badges 1 One possible way to do that is to iterate over a comprehensive list of lists. For example, if you want to obtain something like
which produces
is by using
Maybe, sometimes one more line is not so bad. answered Sep 9, 2021 at 13:52
Not the answer you're looking for? Browse other questions tagged python for-loop or ask your own question.If you’re moving to Python from C or Java, you might be confused by Python’s In this article I’ll compare Python’s For loops in other languagesBefore we look at Python’s loops, let’s take a look at a for loop in JavaScript:
This JavaScript loop looks nearly identical in C/C++ and Java. In this loop we:
Looping in PythonNow let’s talk about loops in Python. First we’ll look at two slightly more familiar looping methods and then we’ll look at the idiomatic way to loop in Python. whileIf we wanted to mimic the behavior of our traditional C-style
This involves the same 4 steps as the This method of looping in Python is very uncommon. range of lengthI often see new Python programmers attempt to recreate traditional
This first creates a range corresponding to the indexes in our list ( This provides us with the index of each item in our for-in: the usual wayBoth the while loop and range-of-len methods rely on looping over indexes. But we don’t actually care about the indexes: we’re only using these indexes for the purpose of retrieving elements from our list. Because we don’t actually care about the indexes in our loop, there is a much simpler method of looping we can use:
So instead of retrieving the item indexes and looking up each element, we can just loop over our list using a plain for-in loop. The other two methods we discussed are sometimes referred to as anti-patterns because they are programming patterns which are widely considered unidiomatic. What if we need indexes?What if we actually need the indexes? For example, let’s say we’re printing out president names along with their numbers (based on list indexes). range of lengthWe could use
But there’s a more idiomatic way to accomplish this task: use the enumeratePython’s built-in
The This function is meant for solving the task of:
So whenever we need item indexes while looping, we should think of Note: the What if we need to loop over multiple things?Often when we use list indexes, it’s to look something up in another list. enumerateFor example, here we’re looping over two lists at the same time using indexes to look up corresponding elements:
Note that we only need the index in this scenario because we’re using it to lookup elements at the same index in our second list. What we really want is to loop over two lists simultaneously: the indexes just provide a means to do that. zipWe don’t actually care about the index when looping here. Our real goal is to loop over two lists at once. This need is common enough that there’s a special built-in function just for this. Python’s
The Note that Looping cheat sheetHere’s a very short looping cheat sheet that might help you remember the preferred construct for each of these three looping scenarios. Loop over a single list with a regular for-in:
Loop over multiple lists at the same time with
Loop over a list while keeping track of indexes with
In SummaryIf you find yourself tempted to use In fact, if you find yourself reaching for
If you find yourself struggling to figure out the best way to loop, try using the cheat sheet above. Practice makes perfectYou don’t learn by putting information in your head, you learn by attempting to retrieve information from your head. So you’ve just read an article on something new, but you haven’t learned yet. Write some code
that uses If you’d like to get hands-on experience practicing Python every week, I have a Python skill-building service you should consider joining. If you sign up for Python Morsels I’ll give you a Python looping exercise that right now and then I’ll send you one new Python exercise every week after that. Fill out the form above to sign up for Python Morsels, get some practice with the Can you have 2 variables in a for loop Python?The use of multiple variables in a for loop in Python can be applied to lists or dictionaries, but it does not work for a general error. These multiple assignments of variables simultaneously, in the same line of code, are known as iterable unpacking.
Can you have two arguments in a for loop?With two arguments in the range function, the sequence starts at the first value and ends one before the second argument. Programmers use x or i as a stepper variable.
How do you iterate through an index in Python?Loop through a list with an index in Python. Using enumerate() function. The Pythonic solution to loop through the index of a list uses the built-in function enumerate(). ... . Using range() function. Another way to iterate over the indices of a list can be done by combining range() and len() as follows: ... . Using zip() function.. How do you pass two arguments in a for loop in Python?If you just want to loop simultaneously, use: for i, j in zip(range(x), range(y)): # Stuff... Note that if x and y are not the same length, zip will truncate to the shortest list. As @abarnert pointed out, if you don't want to truncate to the shortest list, you could use itertools.
|