0
0

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 1 year has passed since last update.

特定のリストから別のリストに存在する要素を全て取り除いた

Last updated at Posted at 2022-05-28

課題

リストAからリストBに含まれる要素を除きたい

検索結果

リスト中の重複削除のみだった

検索ワード

list 重複削除

参考資料

https://codechacha.com/ja/python-list-remove-duplicates/
https://note.nkmk.me/python-list-unique-duplicate/

コード

delete_reject_list.py
def unique_list_make(input_base_list, input_reject_list):
    # リストA中の重複削除
    # set()を使うと高速化できるが、リストの順番が変わるためdictを使用した
    base_list = list(dict.fromkeys(input_base_list))
    # リストB中の重複削除
    reject_list = list(dict.fromkeys(input_reject_list))

    # リストAからリストBをfor文で削除
    result = []

    for i in base_list:
        if i not in reject_list:
            result.append(i)
    
    return result

実行結果

#入力
a = [1,2,3]
b = [3,4,5]

unique_list_make(a,b)

#出力
[1,2]
0
0
4

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?