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

Time:11-09

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()

CodePudding user response:

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)

CodePudding user response:

Thanks for this is helpful! However, when I run it, I still do not get exactly what I want.

The final output that I have in mind is this:

web1 entry1 conten1 conten2 entry2 conten1 conten2 conten3 entry3 conten1 conten2 conten3 conten4 web2 entry1 conten1 conten2 conten3 conten4 conten5 ... content_in_each_entry = [2, 3, 4, 5, 6]

for k in range (1, 3): print(f'web{k}') for x in range (1, 4): print(f'entry{x}') for i in content_in_each_entry: for j in range(1, i 1): print(f'conten{j}')

  • Related