I am trying to remove a value from the list but python seems to not be able to even though I am able to print it. The value is 5 which you will be able to see in the error but when I try to remove the same value it throws me an error message.
The "sequence.remove(e 1)
" line should be the same value as "print(sequence[e 1])
" right?
code:
def solution(sequence):
decisionList = []
ultDec = bool
def comparing():
i = 0
decisionList.clear()
for i in range(len(sequence)-1):
num1 = i
num2 = i 1
if sequence[num1] < sequence[num2]:
decisionList.append("bigger")
if sequence[num1] > sequence[num2]:
decisionList.append("smaller")
if sequence[num1] == sequence[num2]:
decisionList.append("equal")
findIrregular()
repeated = 0
def findIrregular():
nonlocal repeated
ultDec = ""
e = 0
for e in range(len(decisionList)):
val1 = e
if decisionList[val1] == "smaller":
if repeated < 1:
print(sequence[e 1], "is the value im trying to remove")
sequence.remove(e 1)
repeated = repeated 1
comparing()
if repeated >= 1:
return False
elif decisionList[val1] == "equal":
if repeated < 1:
sequence.remove(e 1)
repeated = repeated 1
comparing()
if repeated >= 1:
return False
elif decisionList[val1] == "bigger":
pass
return True
comparing()
return findIrregular()
solution([3, 6, 5, 8, 10, 20, 15])
error:
So as you can see "5 is the value im trying to remove
" is what causes the error
5 is the value im trying to remove
Traceback (most recent call last):
File "<string>", line 46, in <module>
File "<string>", line 44, in solution
File "<string>", line 16, in comparing
File "<string>", line 29, in findIrregular
ValueError: list.remove(x): x not in list
CodePudding user response:
list.remove removes the given member of the list, for example, if we have a list like [1, 2, 5] and we use .remove(5) our list will be [1, 2]. If you want to remove a member by its index, try .pop(i)
CodePudding user response:
you can use
sequence.remove(sequence[e 1])
or
sequence.pop(e 1)
CodePudding user response:
Well, remove
takes the item/value you want to remove. If you want to remove by index then use .pop(index)
Instead of this:
sequence.remove(e 1)
Use:
sequence.pop(e 1)