Python : How to split a list into batch of specific number in python

python @

Here I have a list of 5500 elements , I want to split in to batches of 1000. You can use a list comprehension with slicing to split a list of URLs into batches of 1000. Here’s an example:

urls = ['url1', 'url2', 'url3', ..., 'url5500']  # Your list of 5500 URLs
batch_size = 1000
batches = [urls[i:i + batch_size] for i in range(0, len(urls), batch_size)]

In this example, batches will be a list of lists, where each inner list contains a batch of up to 1000 URLs. The last batch may have fewer than 1000 URLs if the total number of URLs is not evenly divisible by the batch size.

You can then iterate over batches to process each batch of URLs:

for batch in batches:
    # Process the batch of URLs (e.g., download, scrape, etc.)
    for url in batch:
        # Process the individual URL

Refer more on python here :

Author: user

Leave a Reply