Python Forum
delete identical entries in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: delete identical entries in a list (/thread-5096.html)

Pages: 1 2


delete identical entries in a list - Pedroski55 - Sep-18-2017

I have a list of student numbers which looks like this (except the numbers are like 1625010201 and the list is longer):

['123','123','123','456', '456', '456', '789', '789', '789']

What is the best way to get rid of identical entries, so that my list then will look like:

['123', '456', '789']

I've been trying various loops but I can't seem to get it right. Grateful for any tips!


RE: delete identical entries in a list - Larz60+ - Sep-18-2017

show what you have tried so far, and we can help fix.


RE: delete identical entries in a list - Pedroski55 - Sep-18-2017

Quote:errors = studentNumbersWithErrors
for i in range(0, len(errors)):
for j in range(1, len(errors)):
if errors[i] == studentNumbersWithErrors[j]:
del studentNumbersWithErrors[j]


Traceback (most recent call last):
File "<pyshell#94>", line 3, in <module>
if errors[i] == studentNumbersWithErrors[j]:
IndexError: list index out of range



RE: delete identical entries in a list - ichabod801 - Sep-19-2017

Post your code in python tags, not quote tags. See the BBCode link in my signature below for instructions.

Don't loop over indexes to lists, loop over the lists directly. Your error is exactly why you don't do that. j is looping over the indexes of errors when it should be looping over the indexes of studentNumbersWithErrors.


RE: delete identical entries in a list - metulburr - Sep-19-2017

>>> list(set(['123','123','123','456', '456', '456', '789', '789', '789']))
['123', '789', '456']
>>> 



RE: delete identical entries in a list - Pedroski55 - Sep-19-2017

Fantastic! Works like a dream! I had 36 entries in studentNumbersWithErrors, now I have only 4!

Thank you very much! I really am just a raw beginner, I'm very grateful for your help!

The program I use to mark multiple choice is very good, but sometimes a read error slips in. All the cells I look at should contain an entry like A B C or D whatever. If the entry is '' or None, then I can look just at that student's answer form to see what the problem is. That's what I need this for.

Thanks again!


RE: delete identical entries in a list - Skaperen - Sep-19-2017

whenever i have a list of values where order doesn't matter or normal sorted order is usable, i just use a set() to store them.  it's just logical.


RE: delete identical entries in a list - Mekire - Sep-19-2017

(Sep-19-2017, 01:33 AM)metulburr Wrote:
>>> list(set(['123','123','123','456', '456', '456', '789', '789', '789']))
['123', '789', '456']
>>> 
As long as OP is aware that this technique only works for hashable objects.


RE: delete identical entries in a list - snippsat - Sep-19-2017

If order is needed.
>>> from collections import OrderedDict
>>> lst = ['123','123','123','456', '456', '456', '789', '789', '789']
>>> list(OrderedDict.fromkeys(lst))
['123', '456', '789']
In Python 3.6 are regular dict now both ordered and compact.
So then can be used like this.
>>> list(dict.fromkeys(lst))
['123', '456', '789']



RE: delete identical entries in a list - wavic - Sep-19-2017

The dict order in v. 3.6 is the order of the data input.