36
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

既存のlistに複数の値を一度に追加したいときは「+=」または「extend()」を使うと1行で書ける

Last updated at Posted at 2017-12-06

この記事は Pythonのコードを短く簡潔に書くテクニック Advent Calendar 2017 の7日目です。

はじめに

既存のlistオブジェクトに値を追加したいときにappendを使いますが、一度に複数の値を追加したい時は+=またはextend()を使うと1行で書けてスッキリします。

append()で追加する場合

l.append(1)
l.append(2)
l.append(3)

+=で追加する場合

l += [1, 2, 3]

extend()で追加する場合

l.extend([1, 2, 3])

参考

36
24
3

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
36
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?