LoginSignup
0
1

More than 1 year has passed since last update.

【Python】Listに値を一括で追加したいときは 「extend()」 を使用する

Posted at

やりたかったこと

Listに値を一括で追加したい(List同士を結合したい)

INPUT

list_hoge = [2, 3, 5]
list_fuga = [7, 11, 13]

期待するOUTPUT

[2, 3, 5, 7, 11, 13]

append()を使用してみる

ソースコード

list_hoge = [2, 3, 5]
list_fuga = [7, 11, 13]
list_hoge.append(list_fuga)
print(list_hoge)

結果

「List」として追加されてしまったため、期待するOUTPUTとは異なる形になってしまった

[2, 3, 5, [7, 11, 13]]

extend()を使用してみる

ソースコード

list_hoge = [2, 3, 5]
list_fuga = [7, 11, 13]
list_hoge.extend(list_fuga)
print(list_hoge)

結果

期待したOUTPUTと同じ形になった

[2, 3, 5, 7, 11, 13]

参考にしたもの

Python3 ドキュメント

0
1
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
1