Irfan
Irfan

Reputation: 43

Python - Split a list of integers into positive and negative

I'm learning python and i wanted to know whats the way to to split a list like :

A = [1, -3, -2, 8, 4, -5, 6, -7]

into two lists, one containing positive and the other one containing negative integers :

B = [1, 8, 4, 6]
C = [-3, -2, -5, -7]

Upvotes: 4

Views: 16562

Answers (6)

Upendra Varma
Upendra Varma

Reputation: 11

we can use lambda as following:

list(filter(lambda x: 0 > x, A))
Output: [-3, -2, -5, -7]

list(filter(lambda x: 0 <= x, A))
Output: [1, 8, 4, 6]

we have to change the equation(0 <= x) to (0 < x) to avoid 0 in positive numbers list.

Upvotes: 0

Joe
Joe

Reputation: 1

You could also do it using conditionals in list comprehension like this:

k = [1, -3, 5, 9, -1, 3, -3, -2]
positive = list()
negative = [i for i in k if i < 0 or (i > 0 and positive.append(i))]

I'm pretty sure this is O(n) and this is only two lines.

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107347

You can do this in O(n) using a defaultdict():

In [3]: from collections import defaultdict

In [4]: d = defaultdict(list)

In [5]: for num in A:
   ...:     if num < 0:
   ...:         d['neg'].append(num)
   ...:     else: # This will also append zero to the positive list, you can change the behavior by modifying the conditions 
   ...:         d['pos'].append(num)
   ...:         

In [6]: d
Out[6]: defaultdict(<class 'list'>, {'neg': [-3, -2, -5, -7], 'pos': [1, 8, 4, 6]})

Another way is using two separate list comprehensions (not recommended for long lists):

>>> B,C=[i for i in A if i<0 ],[j for j in A if j>0]
>>> B
[-3, -2, -5, -7]
>>> C
[1, 8, 4, 6]

Or as a purely functional approach you can also use filter as following:

In [19]: list(filter((0).__lt__,A))
Out[19]: [1, 8, 4, 6]

In [20]: list(filter((0).__gt__,A))
Out[20]: [-3, -2, -5, -7]

Upvotes: 7

Terrymight
Terrymight

Reputation: 79

def manipulate_data(alist):
      fo = []
      go = []
      for i in alist:
            if i < 0:
                  fo.append(i)
          #elif(i > 0):
               #    go.append(i)
      print fo
    for i in alist:
            if i > 0:
                  go.append(i)
      print go

def main():
      alist = [54,26,-93,-17,-77,31,44,55,20]
      manipulate_data(alist)

if __name__ == '__main__':
      main()

Upvotes: 0

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11695

we can use a function to separate +ve numbers and -ve numbers with a single for loop

def separate_plus_minus(numbers_list):
    positive = []
    negative = []
    for number in numbers_list:
        if number >= 0:
             positive.append(number)
        else:
             negative.append(number)
    return positive, negative

usuage:

numbers_list = [-1,5,6,-23,55,0,25,-10]
positive, negative = separate_plus_minus(numbers_list)

Output:

positve = [5, 6, 55, 0, 25]
negative = [-1, -23, -10]

Upvotes: 0

Krzysztof Kozielczyk
Krzysztof Kozielczyk

Reputation: 5937

If you can afford iterating over A twice, list comprehensions are best IMO:

B = [x for x in A if x >= 0]
C = [x for x in A if x < 0]

Of course there's always the "manual" way:

A = [1, -3, -2, 8, 4, -5, 6, -7]
B = []
C = []
for x in A:
    if (x >= 0):
        B.append(x)
    else:
        C.append(x)

Upvotes: 0

Related Questions