7
5

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 3 years have passed since last update.

listの中にlistを入れたい!

Last updated at Posted at 2019-12-20

#今回の目的:Pythonのlist配列の中にlist配列を入れ込む
#####+=とappend()

#外側のリスト配列の作成
db=[]
#内側のリストの個数をxに入れる
#手法1appendを使う
x=10
[db.append([])for _ in range(x)]
print(db)

結果:
[[], [], [], [], [], [], [], [], [], []]

appendは+=と違い,配列の一番後ろに要素を追加してくれます

また,以下の方法でも同じことができます
(コメントで教えていただきました)

#外側のリスト配列の作成
db=[]
#内側のリストの個数をxに入れる
#手法2
x=10
db = [[] for _ in range(x)]
print(db)

結果:
[[], [], [], [], [], [], [], [], [], []]

こちらのほうがすっきりしていますね

7
5
2

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
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?