9
8

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.

Pythonでリストをマージする

Last updated at Posted at 2018-07-22

リストを結合してマージしたかったので検索結果をメモ

方法

リスト同士の結合後に集合(set)に入れることで重複を削除します。
順番は移動するので順番に意味がある場合は別の方法で実装する必要があります。

# リストを定義
list1 = [2, 3, 5, 7, 11, 17, 19, 23]
list2 = [2, 3, 7, 43]

# リスト同士の結合
list1.extend(list2)
print(list1)

# 重複を除外する
set1=set(list1)
print(set1)
[2, 3, 5, 7, 11, 17, 19, 23, 2, 3, 7, 43]
{2, 3, 5, 7, 11, 43, 17, 19, 23}

参考URL

Python Tips:リストから重複した要素を削除したい - Life with Python

9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?