I have a list li = [1,2,3,4,5,6,7,8,9]
How do I form a nested list for a given range?
lets say if the range is 3 I want the output as [[1,2,3][4,5,6][7,8,9]]
>Solution :
Here’s how you can do it:
li = [1,2,3,4,5,6,7,8,9]
n = 3
new_li = [li[i:i+n] for i in range(0,len(li),n)]
Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]