Python Forum
delete identical entries in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete identical entries in a list
#1
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!
Reply
#2
show what you have tried so far, and we can help fix.
Reply
#3
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
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
>>> list(set(['123','123','123','456', '456', '456', '789', '789', '789']))
['123', '789', '456']
>>> 
Recommended Tutorials:
Reply
#6
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!
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(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.
Reply
#9
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']
Reply
#10
The dict order in v. 3.6 is the order of the data input.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020