Posts: 1,094
Threads: 143
Joined: Jul 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!
Posts: 12,050
Threads: 487
Joined: Sep 2016
show what you have tried so far, and we can help fix.
Posts: 1,094
Threads: 143
Joined: Jul 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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Sep-19-2017, 01:33 AM
(This post was last modified: Sep-19-2017, 01:33 AM by metulburr.)
>>> list(set(['123','123','123','456', '456', '456', '789', '789', '789']))
['123', '789', '456']
>>>
Recommended Tutorials:
Posts: 1,094
Threads: 143
Joined: Jul 2017
Sep-19-2017, 04:22 AM
(This post was last modified: Sep-19-2017, 04:24 AM by Pedroski55.)
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!
Posts: 4,655
Threads: 1,498
Joined: Sep 2016
Sep-19-2017, 05:13 AM
(This post was last modified: Sep-19-2017, 05:13 AM by Skaperen.)
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.
Posts: 591
Threads: 26
Joined: Sep 2016
Sep-19-2017, 01:27 PM
(This post was last modified: Sep-19-2017, 01:28 PM by Mekire.)
(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.
Posts: 7,326
Threads: 123
Joined: Sep 2016
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']
Posts: 2,953
Threads: 48
Joined: Sep 2016
The dict order in v. 3.6 is the order of the data input.
|