-2

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.

user2699
  • 2,927
  • 14
  • 31
Drunk Codes
  • 39
  • 2
  • 7
  • Also the [`unique_everseen`](https://docs.python.org/2/library/itertools.html) example from the docs. Also, it's probably better to choose one of the 2.7 and 3.x tags if the version matters to you. – Useless May 21 '18 at 16:55

2 Answers2

1

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
Dan
  • 527
  • 4
  • 16
  • The slowness part is not necessarily true. In Python 3.6 on my Linux laptop, way #1 takes 542 ns, and way #2 takes 667 ns (measured using %%timeit on a Jupyter notebook). I also measured using the original `dup_list` which actually has duplicates, and the results are 696 ns for way #1 and 1040 ns for way #2. Lesson: built-in operations are usually faster than a pure Python loop. Although the loop performs surprisingly well here, too. Just for fun, I tried them on PyPy as well. Still way #1 wins with 43 ns against 164 ns for way #2. – akaihola May 21 '18 at 17:17
  • Yes they're both quite slow but I just forgot to mention the slowness for the second one. – Dan May 21 '18 at 17:18
  • Asymptotically, Way#1 is O(n) whereas Way#2 is O(n^2) because the test `if not(i in listA):` is linear in the length of listA. So, in the general case, Way#1 is faster and the preferred way. – mcoav May 21 '18 at 17:20
0

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