How can I yield multiple items at a time from an iterable object?
For example, with a sequence of arbitrary length, how can I iterate through the items in the sequence, in groups of X consecutive items per iteration?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your question is a bit vague, but check out the
grouperrecipe in theitertoolsdocumentation.(Zipping the same iterator several times with
[iter(iterable)]*nis an old trick, but encapsulating it in this function avoids confusing code, and it is the same exact form and interface many people will use. It’s a somewhat common need and it’s a bit of a shame it isn’t actually in theitertoolsmodule.)