0
How do you "group" the elements in a list by a given integer?
Example: [a,b,c,d,e] Integer: 2 (group by twos) Output: [[a,b],[c,d],[e]]
2 Risposte
+ 1
[list[i:i+2] for i in range(0,len(list),2)]
+ 1
def tomtx(lst,n):
return [lst[i:i+n] for i in range(0, len(lst),n)]
lst = ["a","b","c","d","e"]
mx = tomtx(lst,2)
print(mx)