

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Reversing negative and positive numbers in JavaScript
- Python program to count positive and negative numbers in a list
- Count positive and negative numbers in a list in Python program
- Java Program to convert positive int to negative and negative to positive
- 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?
- Sum a negative number (negative and positive digits) - JavaScript
- Python - Sum negative and positive values using GroupBy in Pandas
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Map function and Lambda expression in Python to replace characters
- Golang Program to Print the Sum of all the Positive Numbers and Negative Numbers in a List
Advertisements