/  Technology   /  Splitting a List into equal Chunks in Python

Splitting a List into equal Chunks in Python

There are cases where you want to split a list into smaller chunks or you want to create a matrix in python using data from a list. In this article we have specified multiple ways to Split a List, you can use any code example that suits your requirements.

 

1. Using list comprehension

 

This way is recommended when working on simpler tasks.You can use methods and encapsulate these tasks inside the methods.

 

Example:

 

#splitting list into even chunks using list comprehension
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']
n = 3
[my_list[i:i + n] for i in range(0, len(my_list), n)]

 

Output:

 

Splitting a List into equal Chunks in Python

 

2. Using user-defined method

 

To yields successive n-sized chunks where n refers to the number at which a split need to be made we can define a method that iterates over the list.

 

Example:

 

#splitting list into even chunks using user defined method
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']

# defining the function
def split_into_chunks(l, n):
  for i in range(0, len(l), n):
    yield l[i:i + n]   # produces successive n-sized chunks of list

print(list(split_into_chunks(my_list, 3)))

 

Output:

 

Splitting a List into equal Chunks in Python

 

3. Using itertools

 

The itertools method provides a generator that requirements to be iterated over with the help of a for loop.

 

Example:

 

#splitting list into even chunks using itertools
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven']

from itertools import zip_longest
def group_elements(n, iterable, padvalue=None):
  return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue)

for i in group_elements(3,my_list):
  print(i)

 

Output:

 

Splitting a List into equal Chunks in Python

 

As you can see in the above result there are eleven items and hence to slice the list evenly it has taken none in the last row

 

4. Using lambda and islice

 

A lambda and slice method together give a generator that needs to be iterated over. The islice method is utilised to print precise values present in an iterable container.

 

Example:

 

#splitting list into even chunks using lambda and islice
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']

from itertools import islice
def group_elements(it, size):
  it = iter(it)
  return iter(lambda: tuple(islice(it, size)), ())

for i in group_elements( my_list , 3):
  print(i)

 

Output:

 

Splitting a List into equal Chunks in Python

 

5. Using a lambda function

 

We can also use a simple lambda function chunk the data into a particular size.

 

Example:

 

#splitting list into even chunks using lambda
my_list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve']
n = 4
chunk_data = lambda my_list, n: [my_list[x: x+n] for x in range(0, len(my_list), n)]
chunk_data(my_list, n)

 

Output:

 

Splitting a List into equal Chunks in Python

 

In the above example if you can observe depending on the value of n slicing takes place.

 

In this article we have discussed a variety of ways in which a matrix like structure can be created without help of Numpy.

 

 

Leave a comment