element
is an index, not an element from myList
. I would rename your current element
variable to something like index
and then add element = myList[index]
at the top of your while loop:
def splitList2(myList, option):
nList = []
index = 0
while index < len(myList):
element = myList[index]
if option == 0:
if abs(element) > 5:
nList.append(element)
elif option == 1:
if element % 2:
nList.append(element)
index = index + 1
return nList
Of course it would be simpler to just use a for element in myList
loop here instead of your while loop.
solved trouble understanding this while loop [duplicate]