What is the fastest and shortest method to turn this:
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for example into this:
ids = [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
by giving the input 2 as the fixed length.
Of course there are some easy ways to make this but none of them occur to me as fast or pretty.
Why don't you try out a list comprehension?
Example:
[ids[i:i+2] for i in range(0,len(ids),2)]
Output:
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With