0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】zip()関数、zip_longest()関数

Posted at

長さが異なるイテラブルオブジェクトから値を一つずつ取得する際、長さが短いほうに合わせる場合はzip()関数を使い、長いほうに合わせる場合はzip_longest()関数を使います。

zip_longest()関数にfillvalueの引数を付けると、オブジェクトが無い場合の値を設定できます。fillvalueの引数を付けない場合はNoneがセットされます。

zip()関数もzip_longest()関数も、イテラブルオブジェクトが辞書の場合、値ではなくキーを取得します。また、リストではなくタプルのイテレーターが返ります。

なお、zip()関数は組み込み関数のためitertoolsモジュールをインポートする必要はありませんが、zip_longest()関数を使用する場合はインポートする必要があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?