+ 1

why does this code print [1, 0]?

def find_uniq(arr): n = [] for num in arr: if num != arr[0]: n.append(num) arr.remove(num) else: continue if len(n) == 1: return n else: return arr print(find_uniq([ 1, 0, 0, 0 ]))

7th Jul 2020, 2:45 PM
Kirill
Kirill - avatar
7 Antworten
+ 2
madeline no problem, iam also learning, thank you for even taking your time, that means a lot
7th Jul 2020, 4:38 PM
Kirill
Kirill - avatar
+ 2
~ swim ~ i dont know if it is the most practical way to do it or not but it worked perfectly for me so thank you.
7th Jul 2020, 4:44 PM
Kirill
Kirill - avatar
+ 1
i think it has to do with the index reference arr[0]... try this: def find_uniq(arr): n = [] if len(arr) == 1: return arr else: for x in range(0, len(arr)-1): if arr[x] != arr[x-1]: n.append(arr[x]) arr.remove(arr[x]) else: return n
7th Jul 2020, 3:09 PM
madeline
madeline - avatar
+ 1
~ swim ~ that was the best way you could explain the problem, but iam not quite sure how to fix it, can you help me with that?
7th Jul 2020, 4:20 PM
Kirill
Kirill - avatar
+ 1
madeline it doesnt seem to work, for example when the input is [2, 2, 2, 1, 2] it returns an empty list
7th Jul 2020, 4:33 PM
Kirill
Kirill - avatar
+ 1
maybe try to add “if len(arr) == 1:“ after the last else? sorry im still learning 😳
7th Jul 2020, 4:36 PM
madeline
madeline - avatar
0
Kirill Vidov no problem at all!!!
7th Jul 2020, 4:39 PM
madeline
madeline - avatar