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]]

19th Aug 2020, 10:51 AM
Yves Manalo
Yves Manalo - avatar
2 Risposte
+ 1
[list[i:i+2] for i in range(0,len(list),2)]
19th Aug 2020, 11:00 AM
[][]
+ 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)
19th Aug 2020, 11:04 AM
Bahhaⵣ
Bahhaⵣ - avatar