Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array. The order of appearance should be maintained.
Examples:
Input : arr[] = [12, 11, 13, 5, 6, 7, 5, 3, 6] Output : arr[] = [13, 5, 7, 3, 6, 12, 11, 6, 5] Input : arr[] = [12, 11, 0, 5, 6, 7, 5, 3, 6] Output : arr[] = [12, 5, 7, 3, 6, 11, 0, 6, 5]
This problem has many solutions please refer Rearrange positive and negative numbers link, but we will solve this problem with single line of code in python using Lambda Expression.
# Function to rearrange positive and negative elements def Rearrange(arr):
# First lambda expression returns list of negative numbers
# in arr.
# Second lambda expression returns list of positive numbers
# in arr.
return [x for x in arr if x < 0 ] + [x for x in arr if x > = 0 ]
# Driver function if __name__ = = "__main__" :
arr = [ 12 , 11 ,  13 ,  5 , 6 ,  7 , 5 ,  3 ,  6 ]
print (Rearrange(arr))

Output:
arr[] = [13, 5, 7, 3, 6, 12, 11, 6, 5]
This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
 Rearrange positive and negative numbers in O(n) time and O(1) extra space
 Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
 Rearrange positive and negative numbers with constant extra space
 Rearrange positive and negative numbers using inbuilt sort function
 Python  Rearrange Positive and Negative Elements
 Rearrange array in alternating positive & negative items with O(1) extra space  Set 2
 Rearrange array in alternating positive & negative items with O(1) extra space  Set 1
 Map function and Lambda expression in Python to replace characters
 Python  Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
 Intersection of two arrays in Python ( Lambda expression and filter function )
 Only integer with positive value in positive negative value in array
 Find ratio of zeroes, positive numbers and negative numbers in the Array
 Python program to count positive and negative numbers in a list
 Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
 Replace all elements by difference of sums of positive and negative numbers after that element
 C program to count Positive and Negative numbers in an Array
 Move all negative numbers to beginning and positive to end with constant extra space
 Python  Counting sign change in list containing Positive and Negative Integers
 Python  Segregate Positive and Negative Integers from mixed string
 Segregating negative and positive maintaining order and O(1) space
Improved By : nidhi_biet