Pythonで複数のリスト、配列を結合する、ひとつにまとめる方法

hands IT

Pythonのリストは、なんと足し算することで結合することが可能です。

スポンサーリンク

リストの結合

python リスト 連結1
複数のリストを用意しておきます。
#python
リスト1 = ["a","b","c"]
リスト2 = ["d","e","f"]
リスト3 = リスト1 + リスト2
print(リスト3)
このふたつのリストをひとつにまとめるには、ふたつのリストを足し算でつないでやればOKです。
python リスト 連結2
実行結果です。
python リスト 連結3
足し算の左にあるリストほど、結合したリストの頭のほうにきます。
Python操作方法、関数・メソッドリファレンス
使用用途別にまとめたPythonリファレンスページです。 初期準備、基本概念 ダウンロード、インストール、起動 ShellとEditor、保存、実行 保存したPythonの起動 コメント、docstring、行またぎ コメント、...

コメント

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny