itertools.zip_longest()
사용>>> a = zip(*[range(10)]*5, strict=True)
>>> for i in a:
... print(i)
...
(0, 0, 0, 0, 0)
(1, 1, 1, 1, 1)
(2, 2, 2, 2, 2)
(3, 3, 3, 3, 3)
(4, 4, 4, 4, 4)
(5, 5, 5, 5, 5)
(6, 6, 6, 6, 6)
(7, 7, 7, 7, 7)
(8, 8, 8, 8, 8)
(9, 9, 9, 9, 9)
strict
인자를 True로 지정하면 zip 함수에 인수로 넘겨진 이터러블 중 하나가 먼저 소진될 경우 ValueError
를 raise한다(버전 3.10에서 추가됨).
>>> a = [1,2,3,4,5]
>>> b = [1,2,3,4]
>>> for i in zip(a, b, strict=False):
... print(i)
...
(1, 1)
(2, 2)
(3, 3)
(4, 4)
>>> for i in zip(a, b, strict=True):
... print(i)
...
(1, 1)
(2, 2)
(3, 3)
(4, 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is shorter than argument 1
```
---
# 참고자료
- https://docs.python.org/ko/3/library/functions.html?highlight=zip#zip
- https://stackoverflow.com/questions/32954486/zip-iterators-asserting-for-equal-length-in-python
- https://peps.python.org/pep-0618/