How to chunk a list in Python 3?

后端 未结 3 975
遥遥无期
遥遥无期 2020-12-14 06:30

I found the following code that is compatible with python2

from itertools import izip_longest
def grouper(n, iterable, padvalue=None):
  \"grouper(3, \'abcde         


        
3条回答
  •  一整个雨季
    2020-12-14 07:01

    In Python 3's itertools there is a function called zip_longest. It should do the same as izip_longest from Python 2.

    Why the change in name? You might also notice that itertools.izip is now gone in Python 3 - that's because in Python 3, the zip built-in function now returns an iterator, whereas in Python 2 it returns a list. Since there's no need for the izip function, it also makes sense to rename the _longest variant for consistency.

提交回复
热议问题