I'm trying to chunk 100 lists from a nestled list. I have looked looked through multiple examples on Stack Overflow, but I still cannot get something working correctly.

My primary list is named data\_to\_insert and it contains other lists. I would like to extract (chunk) 100 lists from the primary nestled list.

How do I accomplish this?

This is my current code, which doesn't work as needed. 

```
def divide_chunks(l, n):
   for i in range(0, len(l), n):
      yield l[i:i + n]

n = 100
x = list(divide_chunks(data_to_insert, 100)) 

```

Nestled list example:

```
data_to_insert = [['item1','item2','item3','item4','item5','item6'],
 ['item1','item2','item3','item4','item5','item6'],
 ['item1','item2','item3','item4','item5','item6'],
 ['item1','item2','item3','item4','item5','item6'],
 ['item1','item2','item3','item4','item5','item6'],
 ...
 [thousands of others lists go here]]

```

Desired output is another list (sliced\_data), which contains 100 lists from the nestled list (data\_to\_insert). 

```
sliced_data = [['item1','item2','item3','item4','item5','item6'],
 ['item1','item2','item3','item4','item5','item6'], 
 ...
 [98 more lists go here]]

```

I need to loop through the nestled list, data\_to\_insert until it's empty.
['https://Stackoverflow.com/questions/53660611', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6083423/']
If i did understand your question correctly, you need first to flatten your list of list then create a chunks of it. Here is an example using `chain.from_iterable` from `itertools module` along with the code you've used to create chunks:

```
from itertools import chain

def chunks(elm, length):
    for k in range(0, len(elm), length):
        yield elm[k: k + length]

my_list = [['item{}'.format(j) for j in range(7)]] * 1000
flattened = list(chain.from_iterable(my_list))

chunks = list(chunks(flattened, 100))

print(len(chunks[10]))

```

Output:

```
100

```
After some time-consuming research, I developed a solution that worked. The solution below loops through a list of lists and extract 100 lists.

```
# Verifies that the list data_to_insert isn't empty
if len(data_to_insert) > 0:

  # Obtains the length of the data to insert.
  # The length is the number of sublists
  # contained in the main nestled list.
  data_length = len(data_to_insert)

  # A loop counter used in the
  # data insert process.
  i = 0

  # The number of sublists to slice
  # from the main nestled list in
  # each loop.
  n = 100

  # This loop execute a set of statements
  # as long as the condition below is true
  while i < data_length:

    # Increments the loop counter
    if len(data_to_insert) < 100:
      i += len(data_to_insert)
    else:
       i += 100

    # Slices 100 sublists from the main nestled list.
    sliced_data = data_to_insert[:n]

    # Verifies that the list sliced_data isn't empty
    if len(sliced_data) > 0:

      # Removes 1000 sublists from the main nestled list.
      data_to_insert = data_to_insert[n:]

      ##################################
      do something with the sliced_data
      ##################################

      # Clears the list used to store the
      # sliced_data in the insertion loop.
      sliced_data.clear()
      gc.collect()

   # Clears the list used to store the
   # data elements inserted into the
   # database.
   data_to_insert.clear()
   gc.collect()

```