0

I am quite new to python and trying to remove some duplicates from a list following below code. I am getting a list index out of range error which doesn't make sense to me. Thankful for any advice. Cheers Kevin

names = ['Bob','Kenny','Amanda','Bob','Kenny']
num_items = len(names)
print(num_items)
print(range(num_items))
for i in range(num_items):
      counter = names.count(names[i])
      if counter >1:
            names.remove(names[i])
print (names)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-42-926264b9d757> in <module>
      4 print(range(num_items))
      5 for i in range(num_items):
----> 6       counter = names.count(names[i])
      7       if counter >1:
      8             names.remove(names[i])

IndexError: list index out of range
Kevin
  • 51
  • 1
  • 6
  • 1
    It is not recommend to remove the element of list while use for loop. – jizhihaoSAMA Mar 20 '20 at 05:24
  • 5
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – jizhihaoSAMA Mar 20 '20 at 05:24
  • 1
    You can also convert your list to `set` and then back to `list`: `names = list(set(names))` (if order is not important) – Winand Mar 20 '20 at 05:27

5 Answers5

5

just use it like below:

names = ['Bob','Kenny','Amanda','Bob','Kenny']
names = list(set(names))

now names will be list with no duplicates.

PyMaster
  • 1,094
  • 7
  • 11
5

Try this. Let me know if this works

names = ['Bob','Kenny','Amanda','Bob','Kenny']
unique_names = list(set(names))

print(unique_names)
0

What you need is simply a different data structure - a set. Whereas list like [1,2,3,1] is an ordered (we know which is first, which is second, etc) sequence of things, a set is unordered. We only know if something is, or is not in the set. Also, we can get all the elements in the set. And finally, the elements of the set must me unique.

Because of those properties you can simply make a set out of a list (cast list to a set) like set([1,2,3,1]), and you'll get only the unique elements. You can then print it like a list. It's performant and pythonic

butla
  • 695
  • 7
  • 15
0

index out of range occurs because you are using for loop thinking there are n items.

But you remove an item during loop making the number of items to n -1, so when the code is trying to access the nth item, it is not able to see the item in list and thus the error.

names = ['Bob','Kenny','Amanda','Bob','Kenny']

there are 5 items once you remove duplicate

names = ['Bob','Kenny','Amanda','Kenny']

now there are only 4 items with max index of 3.

but you are looping with len(names) which 5 (max index 4)

so names[4] will throw index out of range

that is why you should not remove items when you are iterating it.

so to remove the duplicates you would do something like this

names = ['Bob','Kenny','Amanda','Bob','Kenny']
unique_names = []
for name in names:
  if name not in unique_names:
    uniques_names.append(name)

or you could use sets as mentioned in other answers

Anbarasan
  • 1,197
  • 13
  • 18
0

For this you would definitely use convert it to a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table. If you are looking to continue storing only unique values, it will be best to leave it as a set.

names = ['Bob','Kenny','Amanda','Bob','Kenny']
unique = set(names)
#unique = {'Bob', 'Amanda', 'Kenny'}
unique.add('Bob')
#unique = {'Bob', 'Amanda', 'Kenny'}
#when you are ready to convert it back into a list
names=list(unique)
Bl3nder
  • 82
  • 5