'I am getting index out of error in this in python [duplicate]
I am having an array A=[9, 9, 3, 6, 3].
Why am I getting index out of range error?
def hello(A):
A = set(A)
A = list(A)
A.sort()
for i in range(len(A)):
for j in range(1, len(A)):
if A[j] % A[i] == 0:
A.remove(A[j])
return len(A)
Solution 1:[1]
def hello(A):
A=set(A)
A=list(A)
A.sort()
final_list = A.copy() # copy the list
for i in range(len(A)):
for j in range(1,len(A)):
if(A[j]%A[i]==0):
if A[j] in final_list:
final_list.remove(A[j]) # removed from the copied list instead original list A
return final_list
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
