Help us understand the problem. What is going on with this article?

Python で値の重複を含むリストを値の一意なリストへ[スニペット的備忘録的に]

既存のリスト(一部の値が重複):

['a','a','b','d','d','c','e','c','a']

これを値の一意なリストにしたい:

['a','b','d','c','e']
>>> list1 = ['a','a','b','d','d','c','e','c','a']
>>> list2 = ['dummy'] # 最初にループさせるためのダミー要素.最後に削除
>>> for i in list1:
...     count = 0
...     for j in list2:
...         if j == i: # 作成中の一意リストは重複なく作られているのでここの条件 合致は 0回か 1回
...             count += 1
...     if count == 0: # count がとり得る値は 0 か 1
...         list2.append(i)
... 
>>> list2 = list2[1:] # ダミー要素の削除
>>> print(list2)
['a', 'b', 'd', 'c', 'e']

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away