Home > Back-end >  Associate iterables and items from a list in a for loop
Associate iterables and items from a list in a for loop

Time:12-26

Is it possible to associate certain iterables in a loop with certain items from a list ? I have two lists to start with (totalpages and arguments) and I need to build up certain URL's.

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for i in totalpages:
  pages = list(range(0, x 100, 100))
  print(pages)
  for page, argument in zip(pages, arguments):
    urls = 'http://URL' str(page) argument
    urllst.append(urls)

urllst

I would like urllst to be like :

[
'http://URL0argument1',
'http://URL100argument1',
'http://URL200argument1',
'http://URL300argument1',
'http://URL0argument2'
]

CodePudding user response:

Let me write this answer to express my opinion on using index here.

You were very close to the solution, but zip'ped wrong lists finally. Here's what should work:

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for x, argument in zip(totalpages, arguments):
    for page in range(0, x 100, 100):
        url = f'http://URL{page}{argument}'
        urllst.append(url)

print(urllst)

This iterates over pairs (page_number, argument) made from two initial lists by taking items corresponding to the same indices. I switched to f-string to make string concatenation a bit prettier.

Here's a question about comparing index-based, enumerate-based and zip solutions for such kind of problems.

To ensure that input lists are of equal size, you can use zip(totalpages, arguments, strict=True) - this requires python version 3.10 or newer.

Finally, if you're appending in a loop, you're probably missing an optimisation opportunity: list comprehension would be faster, especially on long inputs.

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = [
    f'http://URL{page}{argument}'
    for x, argument in zip(totalpages, arguments)
    for page in range(0, x 100, 100)
]

print(urllst)

CodePudding user response:

With short zip (to aggregate elements from each of the iterables) itertools.chain.from_iterable (to treat consecutive inner sequences as a single sequence) approach:

import itertools

urllst = list(itertools.chain.from_iterable(
    [f'http://URL{p}{arg}' for p in range(0, page   100, 100)] 
    for page, arg in zip(totalpages, arguments)))

print(urllst)

The output:

['http://URL0argument1', 'http://URL100argument1', 'http://URL200argument1', 'http://URL300argument1', 'http://URL0argument2']

CodePudding user response:

Printing pages after it has been instantiated as a list of numbers will not give you the proper output. It is simpler to just go through each number in a modified nested for-loop:

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for i in range(len(totalpages)):
  curr_arg = arguments[i]
  for x in range(0, totalpages[i] 100, 100):
    urls = 'http://URL' str(x) curr_arg
    urllst.append(urls)

print(urls)
  • Related