Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

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

この記事は 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])

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
20
Help us understand the problem. What are the problem?