Signup/Sign In

Python | Splitting a List into evenly sized Chunks

Posted in Programming   LAST UPDATED: AUGUST 27, 2021

    If you want to split a list into smaller chunks or if you want to create a matrix in python using data from a list and without using Numpy module, you can use the below specified ways. There can be many situations when a list is provided to you, and it needs to be split at specific indices to yield multiple lists, or a if row X column table/matrix needs to be created which will have a fixed number of rows and columns, where every row of data can be visualized as a list. Below we have specified multiple different ways to do so, you can use any code example that suits your requirements.

    1. Using list comprehension

    When working on simpler tasks, this is recommended, but if the requirement is for a bigger application, it is recommended to use methods and encapsulate these tasks inside the methods. This is so that object-oriented concepts are implemented in the right manner.

    Time for an example:

    my_list = [1,2,4,5,6,9,0,34,56,89,31,42]
    n = 3
    [my_list[i:i + n] for i in range(0, len(my_list), n)]

    Output:

    [[1, 2, 4], [5, 6, 9], [0, 34, 56], [89, 31, 42]]
    


    2. Using user-defined method

    A method can be defined that iterates over the list and yields successive n-sized chunks where n refers to the number at which a split needs to be made.

    Time for an example:

    my_list = [1,2,4,5,6,9,0,34,56,89,31,42]
    
    # defining the function
    def split_into_chunks(l, n):
        for i in range(0, len(l), n):
            yield l[i:i + n]   # yields successive n-sized chunks of data
    
    import pprint
    pprint.pprint(list(split_into_chunks(my_list, 3)))

    Output:

    [[1, 2, 4], [5, 6, 9], [0, 34, 56], [89, 31, 42]]
    

    Note: The print() method can be used instead of pprint and the result would be the same. The pprint method in python is just used to ensure that data is printed in a neat and presentable manner.


    3. Using itertools

    The itertools method gives a generator that needs to be iterated over using a for loop.

    Time for an example:

    my_list = [1,2,4,5,6,9,0,34,56,89,31,42]
    
    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:

    (1, 2, 4)
    (5, 6, 9)
    (0, 34, 56)
    (89, 31, 42)
    


    4. Using lambda and islice

    A lambda function in python along with the islice method gives a generator that needs to be iterated over. The islice method is used to selectively print specific values present in an iterable container.

    Time for an example:

    my_list = [1,2,4,5,6,9,0,34,56,89,31,42]
    
    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:

    (1, 2, 4)
    (5, 6, 9)
    (0, 34, 56)
    (89, 31, 42)       


    5. Using a lambda function

    A simple lambda function can also be used to chunk the data into a specific size and split it into a matrix structure or smaller chunks.

    Time for an example:

    my_list = [1,2,4,5,6,9,0,34,56,89,31,42]
    n = 3
    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:

    [[1, 2, 4], [5, 6, 9], [0, 34, 56], [89, 31, 42]]
    


    Conclusion

    In this post, we saw a variety of ways in which a matrix like structure can be created without using Numpy. Do let us know how you would approach this problem in the comment section below.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython List
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS