It's easier to remember with built-in set
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = List (set (L1))
Print L2
There is also a speed difference that is said to be faster and not tested.
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = {}.fromkeys (L1). Keys ()
Print L2
Both have a drawback, and the sorting changes after removing the repeating elements:
[' A ', ' C ', ' B ', ' d ']
If you want to keep their original sort:
Using the Sort method of the list class
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = List (set (L1))
L2.sort (Key=l1.index)
Print L2
Can also be written like this
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = sorted (set (L1), Key=l1.index)
Print L2
You can also use traverse
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = []
For I in L1:
If not I in L2:
L2.append (i)
Print L2
The code above can also be written like this
L1 = [' B ', ' C ', ' d ', ' B ', ' C ', ' A ', ' a ']
L2 = []
[L2.append (i) for I in L1 if not I in L2]
Print L2
This will ensure that the sort is unchanged:
[' B ', ' C ', ' d ', ' a ']
Python list to repeat elements