Mo A
Mo A

Reputation: 21

for Loop: iterating over one value of a list at a time in Python

My problem is that I want to loop over each element of the list one at a time. To elaborate, my list values are, for example, [60, 87, 51]. For the first iteration, I want to iterate over the range of 1 to 60, in the second iteration over the range of 1 to 87, and in the third iteration over the range of 1 to 51.

I tried this code, but every time an error pops-up.

my_list = [60, 87, 51]

driver = webdriver.Chrome()

entries = []
for i in range(1, 4): 
    for j in my_list:
            driver.get('https://plato.stanford.edu/contents.html#a')
            driver.find_element(By.XPATH, f'//*[@id="content"]/ul[1]/li[{i}]/a').click()
            content = driver.find_element(By.XPATH, f'//*[@id="main-text"]/p[{j}]').text
            entries.append([content])
driver.close()

Upvotes: 2

Views: 84

Answers (1)

Jamie.Sgro
Jamie.Sgro

Reputation: 882

This will get the values you're looking for

my_list = [60, 87, 51]
for i in my_list:
   for j in range(1, i+1):
      print(j)

Upvotes: 2

Related Questions