
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Lambda expression in Python to rearrange positive and negative numbers
In this article, we will learn about using lambda expressions that will take an input array of positive and negative integers. We compute two separate arrays one containing negative numbers and other containing positive numbers.
Here we define a Rearrange() function that accepts only one argument i.e. array of integers. The function returns both arrays merged together with each type on different sides of the array.
Now let’s see the code to understand it better.
Example
def Rearrange(arr): # First lambda expression returns a list of negative numbers in arr. # Second lambda expression returns a list of positive numbers in arr. arr_neg=[x for x in arr if x < 0] arr_pos=[x for x in arr if x >= 0] return arr_neg+ arr_pos # Driver function if __name__ == "__main__": arr = [19,-56,3,-1,-45,-23,45,89,90] print (Rearrange(arr))
Output
[-56, -1, -45, -23, 19, 3, 45, 89, 90]
Conclusion
In this article, we learned how to implement lambda expressions to rearrange positive and negative integers in the input array.
- Related Articles
- Lambda expression in Python Program to rearrange positive and negative numbers
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Rearrange positive and negative numbers with constant extra space in C++
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Python program to count positive and negative numbers in a list
- Distinguish positive and negative numbers.
- Reversing negative and positive numbers in JavaScript
- Count positive and negative numbers in a list in Python program
- How to change negative numbers to positive in Excel?
- Explain the difference between positive and negative numbers.
- Java Program to convert positive int to negative and negative to positive
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Map function and Lambda expression in Python to replace characters
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?

Advertisements