Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of Ruby's each_slice(count)

Tags:

python

ruby

What is pythons equivalent of Ruby's each_slice(count)?
I want to take 2 elements from list for each iteration.
Like for [1,2,3,4,5,6] I want to handle 1,2 in first iteration then 3,4 then 5,6.
Ofcourse there is a roundabout way using index values. But is there a direct function or someway to do this directly?

like image 589
theReverseFlick Avatar asked Sep 30 '10 18:09

theReverseFlick


3 Answers

There is a recipe for this in the itertools documentation called grouper:

from itertools import izip_longest
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Use like this:

>>> l = [1,2,3,4,5,6]
>>> for a,b in grouper(2, l):
>>>     print a, b

1 2
3 4
5 6
like image 184
Mark Byers Avatar answered Oct 15 '22 07:10

Mark Byers


I know this has been answered by multiple experts on the language, but I have a different approach using a generator function that is easier to read and reason about and modify according to your needs:

def each_slice(list: List[str], size: int):
    batch = 0
    while batch * size < len(list):
        yield list[batch * size:(batch + 1) * size]
        batch += 1   

slices = each_slice(["a", "b", "c", "d", "e", "f", "g"], 2)
print([s for s in slices])

$ [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g']]

If you need each slice to be of batch size, maybe pad None, or some default character you can simply add padding code to the yield. If you want each_cons instead, you can do that by modifying the code to move one by one instead of batch by batch.

like image 25
nurettin Avatar answered Oct 15 '22 06:10

nurettin


Same as Mark's but renamed to 'each_slice' and works for python 2 and 3:

try:
    from itertools import izip_longest  # python 2
except ImportError:
    from itertools import zip_longest as izip_longest  # python 3

def each_slice(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)
like image 23
bwv549 Avatar answered Oct 15 '22 07:10

bwv549