Splitting a list into N parts of approximately equal length

前端 未结 30 1624
迷失自我
迷失自我 2020-11-22 16:16

What is the best way to divide a list into roughly equal parts? For example, if the list has 7 elements and is split it into 2 parts, we want to get 3 elements in o

30条回答
  •  孤城傲影
    2020-11-22 17:02

    Using list comprehension:

    def divide_list_to_chunks(list_, n):
        return [list_[start::n] for start in range(n)]
    

提交回复
热议问题