1

I have tried the following code to split a list of integers into two lists, but the problem is that I don't know how to make the program deal with each value in the list:

a = [1, 4, 7, 3, 2]
def group(L):
    li = []
    for i in L:
        x= i/2
        if x == int(x):
            li.append(i)
            i.remove(float(i))
    print li
group(a)
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86
Riko
  • 51
  • 6
  • 4
    Is this Python 2 or Python 3? Because `i / 2` has quite different results in both versions. – dhke Apr 18 '17 at 16:47
  • You shouldn't modify a list as you are looping over it, that can cause weird issues. Also, I assume you want `L.remove()`, not `i.remove()`. (Tip: Try to use better variable names than, `L`, and `li`.) – gen_Eric Apr 18 '17 at 16:47
  • @RocketHazmat `L` isn't modified by this code? – dhke Apr 18 '17 at 16:48
  • 2
    python version 2.7 – Riko Apr 18 '17 at 16:48
  • @dhke: I assume `i.remove(float(i))` is supposed to be `L.remove(float(i))`, though I don't know why `float()` is needed here. – gen_Eric Apr 18 '17 at 16:49
  • 2
    @Riko In that case, `i / 2` returns an `int` (round down). Modular division `(i % 2 == 0)` seems to be the established way to test for even/odd. – dhke Apr 18 '17 at 16:51

6 Answers6

3

You can use a tuple to return two lists at once, and the modulo % operator to place elements according to whether they are even or odd (x % 2 is 0 when x is even, and 1 when it is odd).

def group(data):
    result = ([], [])
    for x in data:
        result[x % 2].append(x)
    return result

(even, odd) = group([1, 4, 7, 3, 2])
print(even) #=> [4, 2]
print(odd) #=> [1, 7, 3]
gyre
  • 16,369
  • 3
  • 37
  • 47
  • 2
    ... this even --by Python's indexing magic-- works for negative integers in the input list. – dhke Apr 18 '17 at 16:55
  • @dhke That's because of the way modulus works, not because of indexing magic – sgrg Apr 18 '17 at 16:58
  • @sgrg Hrm. Right, Python's `%` returns positive numbers. I was fixed on C-style modulo. My bad. – dhke Apr 18 '17 at 17:09
3

I would use mod (% 2) to determine even/odd (where even % 2 == 0, odd % 2 == 1. Note that this works mathematically regardless of the sign of the even/odd number being checked):

a=[1,4,7,3,2]
def group(L):
    evens = []
    odds = []
    for elem in L:
        if elem % 2 == 0:
            evens.append(elem)
        else:
            odds.append(elem)
    print evens, odds

# Call group with your input list
group(a)
sgrg
  • 1,210
  • 9
  • 15
1

You can modify your code to make it work by just using two lists (and using the modulo operator, %, is better than checking if x is an instance of int):

a=[1,4,7,3,2]
def group(L):
    l_even=[]
    l_odd = []
    for i in L:
        if i % 2 == 0:
            l_even.append(i)
        else:
            l_odd.append(i)
    print l_even
    print l_odd
group(a)
Kewl
  • 3,327
  • 5
  • 26
  • 45
  • 1
    Not quite. On Python 2 you will get all numbers in `l_even`, since `isinstance(i / 2, int)` if `isinstance(i, int)`. – dhke Apr 18 '17 at 16:52
  • Thanks for the heads up! I'm on python 3 :) Fixed it to just use mod instead – Kewl Apr 18 '17 at 16:53
1

You can take advantage of the fact that the modulus of an even number and 2 is zero, and that the modulus of an odd number and 2 is not zero:

a = [1,4,7,3,2]

def group(numbers):
    evens = []
    odds = []
    for number in numbers:
        if number % 2 == 0:
            evens.append(number)
        else:
            odds.append(number)
    return evens, odds

evens, odds = group(a)
Robert Valencia
  • 1,752
  • 4
  • 20
  • 36
1

You can use the partition recipe from itertools:

from itertools import filterfalse, tee

def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = tee(iterable)
    return filterfalse(pred, t1), filter(pred, t2)

def is_odd(x):
    return x % 2

evens, odds = partition(is_odd, [1, 4, 7, 3, 2])
print(*evens)
print(*odds)

Which prints:

4 2
1 7 3
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0

A lot of the answers here work by using the modulus function. So this is a different approach that detects odd or even integers simply by checking the status of the right-most bit. The solution appends each entry to list even or odd according to status of this bit using this solution. This allows a very concise solution in 3 lines:

even,odd=[],[]
for i in a:
    (even,odd)[i >> 0 & 1].append(i)

Giving the correct solution:

>>> even
[4, 2]
>>> odd
[1, 7, 3]
ClimateUnboxed
  • 7,106
  • 3
  • 41
  • 86