Requirements describe : Will a list on length n Divide equally
for example :
[list_data[i*num:(i+1)*num] for i in range(int(len(list_data)/num) + 1) if list_data[i*num:(i+1)*num]]
The code is as follows :
# demand
# Requirements describe : Will a list on length n Divide equally
#
# for example :
#
# Input list by :
list_data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Average length n by :
num = 3
# Expected output :[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
# 1. One line of code
result = [list_data[i*num:(i+1)*num] for i in range(int(len(list_data)/num) + 1) if list_data[i*num:(i+1)*num]]
# 2. One line function version
def list_equally_split(list_data, num): return [list_data[i*num:(i+1)*num] for i in range(int(len(list_data)/num) + 1) if list_data[i*num:(i+1)*num]]