I have a list containing the strings
a=['boy', 'girl', 'man', 'boy']
Now I want a new list that doesn't have repeated elements
b = ['boy', 'girl', 'man']
How do I do that in Python?
I have a list containing the strings
a=['boy', 'girl', 'man', 'boy']
Now I want a new list that doesn't have repeated elements
b = ['boy', 'girl', 'man']
How do I do that in Python?
from DOC of set
:
|Sets are mutable unordered collections of unique elements.
use set
like below:
a=['boy', 'girl', 'man', 'boy']
list(set(a))
output:
['man', 'boy', 'girl']
by thanks of @jonrsharpe for maintaining of list's order you can try this:
from collections import OrderedDict
a=['boy', 'girl', 'man', 'boy']
list(OrderedDict.fromkeys(a))
output:
['boy', 'girl', 'man']
# import libraries
import numpy as np
a=['boy', 'girl', 'man', 'boy']
b = ['boy', 'girl', 'man']
print("Union of two arrays :", np.union1d(a, b))