I am trying to write a function called splitList(myList, option)
that takes a list and an option which is either 0
or 1
as parameters. If the value of the option is 0
the function returns a list consisting of the elements in myList
that are negative and if the value of the option is 1
the function returns a list consisting of the elements in myList
that are even (we consider 0 to be an even number, since it is evenly divisible by 2).
For example:
splitList([1,-3,5,7,-9,-11,0,2,-4], 0)
Would return the list:
[-3,-9,-11,-4]
Where as:
splitList([1,-3,5,7,-9,-11,0,2,-4], 1)
Would return the list:
[0,2,-4]
For this problem I must use a for loop
.
Here is what I have:
def splitList(myList, option):
negativeValues = []
positiveValues = []
evenValues = []
for i in range(0,len(myList)):
if myList[i] < 0:
negativeValues.append(myList [i])
else:
positiveValues.append(myList [i])
for element in myList:
if option == 1:
myList [i] % 2 == 0
evenValues.append(myList [i])
return evenValues
else:
return negativeValues
The only thing I cannot get it to do is to is sort the list and return all the numbers that are divisible by 2.