python list index out of range isinstance() -
getting indexerror: list out of range when run:
duplicates([0, 1, 2, 3, 4, 5]) when run in shell:
list = [0, 1, 2, 3, 4, 5] list = ['blah', 1, 2, 3] if (isinstance(list[0], str)): list.append(list[0]) list.pop(0) everything works fine.
def duplicates(array): list = [] in array: if (array.count(i) > 1): if (list.count(i) < 1): list.append(i) else: pass # has nothing problem statement. test cases requiring specific output format isn't highlighted in original problem. list.sort() list.reverse() if (isinstance(list[0], str)): list.append(list[0]) list.pop(0) return list yet when run standard function, runs error. don't understand why isinstance() running index out of range here.
the important thing of never name variable list,string,dict etc..
the list variable empty error
it happens here:
list = [] in array: if (array.count(i) > 1): if (list.count(i) < 1): list.append(i) else: pass as per input function there no duplicates first if fails there no value in list
you trying access uncreated index
i.e.)
las=[] las[0] traceback (most recent call last): file "<stdin>", line 1, in <module> indexerror: list index out of range in program:
if (isinstance(list[0], str)): list.append(list[0]) list.pop(0) you checking list[0] empty string error thrown
as per program
you storing duplicate values list variable since input not contain duplicate value shows error
you try
modification:
if (list) and(isinstance(list[0], str)): list.append(list[0]) list.pop(0) it checks list available tries it's first element
Comments
Post a Comment