0
scores = [86,86,85,85,85,83,23,45,84,1,2,0]

def find_h (alist):
    f_list = []
    for item in range (0, len (alist) + 1):
        if alist[item] == alist[item +1]:

            continue


        else:
            f_list.append (alist[item])

    print (f_list)
        #if item not in f_list: 
            #f_list.append (item)



find_h (scores)

The idea is to remove duplicates from, for sake of intuition, what is the best way to tackle this task?

How is it possible to break the condition and stop counting the, adding +1 to (alist[item +1]) because this works to a certain point

the output is: IndexError: list index out of range

EkoNezbit
  • 33
  • 5
  • 1
    The reason for list index out of range is `range(0, len(alist) + 1`, there is no reason for the `+1` because index is starting from `0` till the length. Best way to remove duplicates is using `sets` just do `return list(set(scores)) ` – Gaurav Agarwal Garg Mar 05 '20 at 19:19
  • In general you can convert to a set, then back to a list to remove duplicates. – quamrana Mar 05 '20 at 19:19

2 Answers2

0

You could do it like this, instead of managing the next item in the list (which always puts you in danger of IndexErrors

scores = [86,86,85,85,85,83,23,45,84,1,2,0]
new_scores = []

for score in scores:
    if score not in new_scores:
        new_scores.append(score)
print(new_scores)
Parakiwi
  • 591
  • 3
  • 19
0

Alternate solution is to use Sets object type, which will not store any duplicates.First convert the existing list to set object,then convert it back to list.But sets wont preserve the order of the list.

scores = [86,86,85,85,85,83,23,45,84,1,2,0]
scores=list(set(scores))
fruitbat
  • 88
  • 5