How to remove duplicate items from list in Python

4 Answers

0 votes
lst = [34, 78, 34, 90, 'python', 'java', 7.35, 34, 'python']
  
index = 1
while index < len(lst):
    if lst[index] in lst[ : index]:
        lst.pop(index)
    else:
        index += 1

print(lst) 



'''
run:

[34, 78, 90, 'python', 'java', 7.35]

'''

 



answered Jan 10, 2021 by avibootz
0 votes
lst = [34, 78, 34, 90, 'python', 'java', 7.35, 34, 'python']
  
lst = list(set(lst)) 

print(lst) 



'''
run:

[34, 7.35, 'java', 78, 'python', 90]

'''

 



answered Jan 10, 2021 by avibootz
0 votes
lst = [34, 78, 34, 90, 'python', 'java', 7.35, 34, 'python']
  
lst = [i for v, i in enumerate(lst) if i not in lst[:v]] 

print(lst) 



'''
run:

[34, 78, 90, 'python', 'java', 7.35]

'''

 



answered Jan 10, 2021 by avibootz
0 votes
from collections import OrderedDict 

lst = [34, 78, 34, 90, 'python', 'java', 7.35, 34, 'python']
  
lst = list(OrderedDict.fromkeys(lst)) 

print(lst) 



'''
run:

[34, 78, 90, 'python', 'java', 7.35]

'''

 



answered Jan 10, 2021 by avibootz

Related questions

1 answer 202 views
2 answers 211 views
2 answers 149 views
1 answer 161 views
3 answers 255 views
1 answer 191 views
...