Lambda expression in Python to rearrange positive and negative numbers
Last Updated :
27 Mar, 2023
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.
Implementation:
Approach:
- Define a function named "Rearrange" that takes an array "arr" as an argument.
- Use list comprehension to create two separate lists - one containing all negative elements in "arr" and another containing all non-negative (positive and zero) elements in "arr".
- Concatenate the two lists in the order of negative elements first, followed by non-negative elements.
- Return the concatenated list.
- In the main section, create an array "arr" with some positive and negative numbers.
- Call the "Rearrange" function with "arr" as an argument.
- Print the returned list.
Python3
# 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[-13, -5, -7, -3, -6, 12, 11, 6, 5]
Time Complexity: O(n)
We iterate over the list of elements once, so the time complexity is linear.
Space Complexity: O(n)
We create two temporary lists, so the space complexity is also linear.
Similar Reads
Python | Rearrange Positive and Negative Elements Sometimes, while working with Python lists, we can have a problem in which we need to perform a sort in list. There are many variations of sorting that might be required to perform. Once such variation can be to sort elements of list, but keeping in mind, positive elements appear before negative ele
8 min read
Rearrange positive and negative numbers using inbuilt sort function Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array without using any additional data structure like a hash table, arrays, etc. The order of appearance should be maintained. Examples: Input : arr[] = [12,
15 min read
Rearrange positive and negative numbers with constant extra space Given an array arr[] of integers, the task is to arrange them such that all negative integers appear before all the positive integers in the array without using any additional data structure like a hash table, arrays, etc. The order of appearance should be maintained.Examples: Input: arr[] = [12, 11
15+ min read
Javascript Program to Rearrange positive and negative numbers in O(n) time and O(1) extra space An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If the
3 min read
Rearrange Array in negative numbers, zero and then positive numbers order Given an arr[ ] of size N, containing positive, negative integers and one zero. The task is to rearrange the array in such a way that all negative numbers are on the left of 0 and all positive numbers are on the right. Note: It is not mandatory to maintain the order of the numbers. If there are mult
6 min read
Alternate Rearrangement of Positives and Negatives An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. A number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If t
11 min read