Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through Python list with 2 variables

Tags:

python

In JavaScript, I can use two (2) pointers to get the values from a list at the beginning and end and then increment/decrement the pointers to continue. For example:

for (let i = 0, j = list.length - 1; i < j; i++, j--) {
    console.log(i, j);
}

How would I be able to accomplish the same in Python? I know that I can get the current index by doing:

for index in enumerate(list):
    print(index)

But how could I also initialize another pointer to start from the end?

like image 297
User 5842 Avatar asked Aug 10 '19 19:08

User 5842


People also ask

How to use for loop with multiple variables in Python?

Various combinations of methods can be used with multiple variables using for loop. Using zip () function with for loop for multiple variables. Using for loop with a key-value pair of a dictionary.

How do you loop through a list in Python?

The short answer is to use the for loop to loop over a List variable and print it in the output. You can also use the While loop of Python to traverse through all the elements of the List variable.

What is the use of for loop in Python?

A for loop is used for iterating over any sequence, from a list to a tuple to a dictionary. It can even iterate over a string. This article discusses how to use the for loop for multiple variables in Python.

How to iterate over multiple variables simultaneously in Python?

There's two possible questions here: how can you iterate over those variables simultaneously, or how can you loop over their combination. Fortunately, there's simple answers to both. First case, you want to use zip. x = [1, 2, 3] y = [4, 5, 6] for i, j in zip (x, y): print (str (i) + " / " + str (j))


Video Answer


2 Answers

I assume that you want a solution that can do anything with the indices, not just print them out. Python's for loops have other strengths. So use a while loop.

i = 0
j = len(alist)
while i < j:
    print(i, j)  # or console.log(i, j) or whatever you want here
    i += 1
    j -= 1

Here is something similar using zip, which is more pythonic. Here I illustrate doing something other than just printing the indices.

alist = [3, 1, 4, 1, 5, 9]
llen = len(alist)
for i, j in zip(range(llen // 2), range(llen - 1, -1, -1)):
    print(alist[i], alist[j])

But it is even more pythonic to ignore the indices and just use the items of the list, as in

alist = [3, 1, 4, 1, 5, 9]
llen = len(alist)
for u, v in zip(alist[:llen // 2], alist[::-1]):
    print(u, v)
like image 190
Rory Daulton Avatar answered Nov 10 '22 10:11

Rory Daulton


Here's an example how you can do it. Take second index as function of length minus index minus one:

l = [1, 2, 3, 4]
for i, _ in enumerate(l):
    print(l[i], l[len(l)-i-1])

This will output

(1, 4)
(2, 3)
(3, 2)
(4, 1)

Not printing indexes themselves, but you can print them, if you chose to do so.

like image 31
dmitryro Avatar answered Nov 10 '22 10:11

dmitryro