Split a list into sublists

    0

    2

    Python public recipes

    Split a list into multiple sublists.

    Example ot use

    sublists = chunks(mylist, 3)
    

    will return a split of the initial mylist into lists of at most 3 elements.

    def chunks(lst, n):
        """Yield successive n-sized chunks from lst."""
        for i in range(0, len(lst), n):
            yield lst[i:i + n]
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.