A generator function would be neat:
def batch_gen(data, batch_size):
for i in range(0, len(data), batch_size):
yield data[i:i+batch_size]
Example use:
a = "abcdef"
for i in batch_gen(a, 2): print i
prints:
ab
cd
ef
More Related Contents:
- What is the fastest and pythonic way to generate a list [(0,0), (0,1), (0,2)…(0,100)]? [closed]
- how to can i go over a file twice?
- List Element without iteration
- How to remove items from a list while iterating?
- What exactly are iterator, iterable, and iteration?
- Are for-loops in pandas really bad? When should I care?
- Iterating through a range of dates in Python
- Iterating on a file doesn’t work the second time
- Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?
- Iterating each character in a string using Python
- Loop that also accesses previous and next values
- Iterate over object attributes in python [duplicate]
- What is the perfect counterpart in Python for “while not EOF”
- recursive iteration through nested json for specific key in python
- What are iterator, iterable, and iteration?
- Intersecting two dictionaries
- How to recursively find specific key in nested JSON?
- Is generator.next() visible in Python 3?
- Converting a list of tuples into a dict
- How to loop backwards in python? [duplicate]
- How to transform string of space-separated key,value pairs of unique words into a dict
- Iteration over list slices
- Set iteration order varies from run to run
- Python: Adding element to list while iterating
- How can I collect the results of a repeated calculation in a list, dictionary etc. (make a copy of a list with each element modified)?
- What is the meaning of list[:] in this code? [duplicate]
- What is the cleanest way to get the sum of numbers in a collection/list in Dart?
- Iterate over pairs in a list (circular fashion) in Python
- Customizing unittest.mock.mock_open for iteration
- Iterate over a string 2 (or n) characters at a time in Python