Please how can I delete duplicates in list. Example dup_list = ['he', 'he', 'sh', 'sh', ' jk', 'jk', 'gf', 'gf'] I want the new list to look like this.
new_list = ['he', 'sh', ' jk', 'gf']
Please can some help me with this.
Please how can I delete duplicates in list. Example dup_list = ['he', 'he', 'sh', 'sh', ' jk', 'jk', 'gf', 'gf'] I want the new list to look like this.
new_list = ['he', 'sh', ' jk', 'gf']
Please can some help me with this.
Way #1
new_list = ['he', 'sh', ' jk', 'gf']
new_list = list(set(new_list))
As mentioned to me in the comments, this is generally faster than the second way and is therefore preferred.
Way #2
new_list = ['he', 'sh', ' jk', 'gf']
listA=[]
for i in new_list:
if not(i in listA):
listA.append(i)
new_list = listA
Long story short you can convert it to a set and then back to a list and that would do the work:
dup_list = ['he', 'he', 'sh', 'sh', ' jk', 'jk', 'gf', 'gf']
new_list = list(set(dup_list))
For the complete explanation and other examples there was already a question posted: Get unique values from a list in python