How can I iterate through the python list, and stop to load the next URL before continuing the process? How can I iterate through the python list, and stop to load the next URL before continuing the process? selenium selenium

How can I iterate through the python list, and stop to load the next URL before continuing the process?


It seems like you have code that works for a single list, and now you just want it to work over smaller portions of that list.

Usually you see "convert list of lists into one flat list". This is the opposite.

I'm assuming mfrnumbers is your flat list. We'll create a generator function that given one flattened_list, it returns the list_id and the elements in that list. As stated in your question, you'll figure out how to actually get that list. So for now, I'm assuming the list_id is a simple integer.

This function get_list(mfrnumbers) will return those numbers in groups of max_items_per_list. Technically, it returns an iterator that you will iterate over.

def get_list(flattened_list, max_items_per_list=250):    # maybe you have some pattern for list names?    list_id = 1    while len(flattened_list) > 0:        current_list = flattened_list[:max_items_per_list]        yield list_id, current_list        flattened_list = flattened_list[len(current_list):]        list_id += 1

And we can call this function as follows:

for (myid, mylist) in get_list([1,2,3,4,5], max_items_per_list=2):    print (myid, mylist)

Output:

1 [1, 2]2 [3, 4]3 [5]

So in your case, you would run your entire big loop for number in mfrnumbers as an inner loop but with the output of get_list.

for (myid, mylist) in get_list(mfrnumbers):    # stop and do any loading for this new list...    for number in mylist:       .....