Nov-29-2016, 05:30 PM
"""
Create a function called
N (numeric). The goal of the function would be to split the iterable I into
multiple sublists of length N.
The function must:
1. Split the iterable I into multiple sublists, all of which are N-elements
long (except for the last sublist, which can be shorter, if there are less
than N elements remainig). Return a new list composed of all sublists, in order.
2. When inspected, would return the following help string:
Split an iterable I into sublist of at most N elements
Hint: The built-in
used to specify the step between the iterations
For example:
list_split([1,2,3,4,5,6,7], 2) -> [[1,2],[3,4],[5,6],[7]]
list_split('abcdefghijk', 3) -> ['abc','def','ghi','jk']
"""
Create a function called
list_split
, taking the arguments I (iterable) andN (numeric). The goal of the function would be to split the iterable I into
multiple sublists of length N.
The function must:
1. Split the iterable I into multiple sublists, all of which are N-elements
long (except for the last sublist, which can be shorter, if there are less
than N elements remainig). Return a new list composed of all sublists, in order.
2. When inspected, would return the following help string:
Split an iterable I into sublist of at most N elements
Hint: The built-in
range()
function takes an optional third argument that isused to specify the step between the iterations
For example:
list_split([1,2,3,4,5,6,7], 2) -> [[1,2],[3,4],[5,6],[7]]
list_split('abcdefghijk', 3) -> ['abc','def','ghi','jk']
"""
def list_split(*args): I = args[0] N = args[1] n = max(1, N) return (I[i:i+n] for i in range(0, len(I), n)) x = [1,2,3,4,5,6,7] y = 2 list_split(x,y)what am i doing wrong here why is it not spitting out the result