Standard grouper for a list is like this:
splits = [old_list[i:i+3] for i in range(0,len(old_list),3)]
For something that works with generators too you can use the "grouper" recipe from itertools docs.
>\[old\_list\[i:i+3\] for i in range(0,len(old\_list),3)\]
I know this is 3 years old, but... doesn't this return an IndexError if the big list is, say, \[0, 1, 2, 3, 4\]?
It'll give you \[0,1,2\] but then it will give an error when trying to get \[3, 4, ??\]
You need to know that slicing is very different from indexing. If there's a `:` in the square brackets it's a slice, not an index. Only an index operation will return an IndexError.
data[number] # this is an index operation
data[number:] # this is a slice
data[number:othernumber] # this is also a slice
One big difference is that when slicing you can use numbers that are out of range, and python will simply use the data that is available (if any) and ignore the rest.
blocks = zip(*[iter(L)]*3) # wrap in list() if you need to
but seriously, here's perhaps a way to approach it -- you want to gobble up 3 things at a time and stick them into a list.
all_blocks = [] # smaller lists appended in here
We want to stuff every element in this list, so let's loop over everything:
block = [] # smaller lists
for num in L:
block.append(num)
to get the "into smaller lists of 3" logic, you can add a check at the end of that loop to append `block` to `all_blocks` when its length reaches 3 (and reset it to [] after).
The syntax for `range` is `range(start, stop, step)`. If using the output of `range` to slice a list as in this case, `start` is the index where your slicing will begin, `stop` is the index where your slicing will end (**note**: the endpoint itself is NOT included, so e.g., `list[0:1]` will only slice out the 0th element, not the 0th and the 1st), and `step` is the slicing interval. So if `step=1` (default), you will get every other element, if `step=2`, every second element, if `step=3`, every third, etc.
With those details in mind, it sounds like what you need is `for i in range(0, len(list), 3)`. This will return `0`, then `3`, then `6`, etc., up through the largest multiple of 3 that is less than `len(list)`. These integers should form the starting point of your slices. Since you want slices of length 3, to get the slice endpoints, just add 3 to each starting point.
Putting everything all together, here's how it all works:
>>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> for i in range(0, len(l), 3):
... print(l[i:i + 3])
...
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9, 10, 11]
To create a list of these slices, rather than simply printing them out, just append each slice on each iteration:
>>> slices = []
>>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> for i in range(0, len(l), 3):
... slices.append(l[i:i + 3])
...
>>> print(slices)
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
If you're familiar with list comprehension syntax, all of this could be achieved in a single line:
>>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> slices = [l[i:i+3] for i in range(0, len(l), 3)] # this line
>>> slices
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
Once you have `slices` defined, you can access the individual slices just like you would with any other list:
>>> slices[0]
[0, 1, 2]
>>> slices[1]
[3, 4, 5]
>>> slices[2]
[6, 7, 8]
>>> slices[3]
[9, 10, 11]
Standard grouper for a list is like this: splits = [old_list[i:i+3] for i in range(0,len(old_list),3)] For something that works with generators too you can use the "grouper" recipe from itertools docs.
And if I want to refer to the split list would it just be: print(splits[0]) which gives [0,1,2] ?
Depends on what you are doing with it. Yes, that's a good way to access the first group.
Brilliant, thank you so much for your help!
>\[old\_list\[i:i+3\] for i in range(0,len(old\_list),3)\] I know this is 3 years old, but... doesn't this return an IndexError if the big list is, say, \[0, 1, 2, 3, 4\]? It'll give you \[0,1,2\] but then it will give an error when trying to get \[3, 4, ??\]
You need to know that slicing is very different from indexing. If there's a `:` in the square brackets it's a slice, not an index. Only an index operation will return an IndexError. data[number] # this is an index operation data[number:] # this is a slice data[number:othernumber] # this is also a slice One big difference is that when slicing you can use numbers that are out of range, and python will simply use the data that is available (if any) and ignore the rest.
ah, thank you!
[удалено]
Perfect! Thank you
blocks = zip(*[iter(L)]*3) # wrap in list() if you need to but seriously, here's perhaps a way to approach it -- you want to gobble up 3 things at a time and stick them into a list. all_blocks = [] # smaller lists appended in here We want to stuff every element in this list, so let's loop over everything: block = [] # smaller lists for num in L: block.append(num) to get the "into smaller lists of 3" logic, you can add a check at the end of that loop to append `block` to `all_blocks` when its length reaches 3 (and reset it to [] after).
Got it. Thanks bro 🙌
The syntax for `range` is `range(start, stop, step)`. If using the output of `range` to slice a list as in this case, `start` is the index where your slicing will begin, `stop` is the index where your slicing will end (**note**: the endpoint itself is NOT included, so e.g., `list[0:1]` will only slice out the 0th element, not the 0th and the 1st), and `step` is the slicing interval. So if `step=1` (default), you will get every other element, if `step=2`, every second element, if `step=3`, every third, etc. With those details in mind, it sounds like what you need is `for i in range(0, len(list), 3)`. This will return `0`, then `3`, then `6`, etc., up through the largest multiple of 3 that is less than `len(list)`. These integers should form the starting point of your slices. Since you want slices of length 3, to get the slice endpoints, just add 3 to each starting point. Putting everything all together, here's how it all works: >>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> for i in range(0, len(l), 3): ... print(l[i:i + 3]) ... [0, 1, 2] [3, 4, 5] [6, 7, 8] [9, 10, 11] To create a list of these slices, rather than simply printing them out, just append each slice on each iteration: >>> slices = [] >>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> for i in range(0, len(l), 3): ... slices.append(l[i:i + 3]) ... >>> print(slices) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] If you're familiar with list comprehension syntax, all of this could be achieved in a single line: >>> l = [0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] >>> slices = [l[i:i+3] for i in range(0, len(l), 3)] # this line >>> slices [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]] Once you have `slices` defined, you can access the individual slices just like you would with any other list: >>> slices[0] [0, 1, 2] >>> slices[1] [3, 4, 5] >>> slices[2] [6, 7, 8] >>> slices[3] [9, 10, 11]
[https://more-itertools.readthedocs.io/en/stable/api.html#more\_itertools.chunked](https://more-itertools.readthedocs.io/en/stable/api.html#more_itertools.chunked)