0

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.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
user2149455
  • 3
  • 1
  • 2

5 Answers5

4

Using a loop is a bit redundant here since there's a standard function filter that does what you want: returns a new list with these elements of a list which match a given predicate.

Let's define the predicates first:

def is_even(x):
    return x % 2 == 0

def is_negative(x):
    return x < 0

Then you can easily define your function in terms of filter:

def splitList(myList, option):
    predicate = is_negative if option == 0 else is_even
    return filter(predicate, myList)
Kos
  • 70,399
  • 25
  • 169
  • 233
  • As an exercise, I suggest to write the `filter` function yourself. – Kos Mar 08 '13 at 18:14
  • 2
    you could also use `lambdas` to make it even shorter. I personally prefer an if wich has two return statements over an additional variable but that is a matter of taste. an requirement was that a `for` is used, otherwise I see the beauty in your code... – ted Mar 08 '13 at 18:16
  • 2
    +1 I like your answer, but what do you make of this: "For this problem, you must use a “for loop”." Maybe: `return [x for x in myList if predicate(x)]` – hughdbrown Mar 08 '13 at 18:24
  • You can implement `filter` yourself using a for loop, like I said in my first comment. – Kos Mar 08 '13 at 18:33
  • @hughdbrown using `map` and `filter` is very much like using list comprehensions; I use `map`/`filter` when I have the function ready and a comprehension when I'd have to write a lambda. – Kos Mar 08 '13 at 18:34
  • I'm familiar with `map`, `filter`, list comprehensions, and more. I was just saying that OP's question (from his prof? for homework?) has a requirement for a `for-loop`. Or, I agree: we wouldn't write it with a `for-loop` because we know better, but OP Is asking for it that way for some reason. – hughdbrown Mar 08 '13 at 18:44
2

You can build all your variants from these primitives:

def even_list(numbers):
    return [x for x in numbers if not (x & 1)]

def odd_list(numbers):
    return [x for x in numbers if x & 1]

def negative_list(numbers):
    return [x for x in numbers if x < 0]

def positive_list(numbers):
    return [x for x in numbers if x > 0]

Then test:

>>> def test():
...     numbers = list(range(-3, 4))
...     print even_list(numbers)
...     print odd_list(numbers)
...     print positive_list(numbers)
...     print negative_list(numbers)
... 
>>> test()
[-2, 0, 2]
[-3, -1, 1, 3]
[1, 2, 3]
[-3, -2, -1]

Later: so stealing from @Kos, you could write split_list like this:

def split_list(myList, option):
    predicate = negative_list if not option else even_list
    return predicate(myList)

Or:

def split_list(myList, option):
    predicates = [negative_list, even_list]
    return predicates[option](myList)

Not sure if it meets your needs if the for-loop is in a list comprehension in a called function.

Also: "Function names should be lowercase, with words separated by underscores as necessary to improve readability."

hughdbrown
  • 47,733
  • 20
  • 85
  • 108
  • +1 for list comprehension – ted Mar 08 '13 at 18:21
  • 2
    Didn't see this: 'For this problem, you must use a “for loop”.' Bah, humbug. – hughdbrown Mar 08 '13 at 18:23
  • 1
    well true list comprehension is technically not a for loop one could argue, but I think it is good to see the option and I would count it as close enough, you basically have changed the order. – ted Mar 08 '13 at 18:25
  • 1
    @Ted: "you basically have changed the order" Changed the order -- of what? The numbers are in the same order. – hughdbrown Mar 08 '13 at 18:27
  • 1
    The syntactical order, instead of `loop, check condition, add to list` it reads `make a list from all elements looped over that match condition` – ted Mar 08 '13 at 18:43
1

You return too soon. You first have to complete the foor loop and return after it, not from inside the loop.

Example

for i in range(5):
    print i
    numbers.append(i)
    return numbers   //wrong: exit the function on the first pass in the loop.

for i in range(5):
    print i
    numbers.append(i)

return numbers       //right

Besides that why do you calculate the negative valuse list if you don't need it?

ted
  • 4,791
  • 5
  • 38
  • 84
1
def splitList(myList,option):

    negative_numbers = [i for i in myList if i < 0]
    even_numbers = [i for i in myList if i % 2 == 0]

    return sorted(even_numbers) if option else sorted(negative_numbers)
jurgenreza
  • 5,856
  • 2
  • 25
  • 37
0

I believe this is what you where trying to achieve:

def splitList(myList,option):

    result = []

    if option == 0:
        for item in myList:
            if (item < 0):
                result.append(item)
    elif option == 1:
        for item in myList:
            if (item % 2 == 0):
                result.append(item)
    else:
        return "Option Error"

    return sorted(result)

print splitList([1,-3,5,7,-9,-11,0,2,-4], 0)
print splitList([1,-3,5,7,-9,-11,0,2,-4], 1)
print splitList([1,-3,5,7,-9,-11,0,2,-4], 2)

Outputs:

[-11, -9, -4, -3]
[-4, 0, 2]
Option Error
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202