Python | Get positive elements from given list of lists
Last Updated :
09 May, 2023
Given a list of list, the task is to get only positive element from given list. Below are some ways to solve the above problem.
Method #1: Using Iteration
Python3
Input = [[ 10 , - 11 , 222 ], [ 42 , - 222 , - 412 , 99 , - 87 ]]
Output = []
for elem in Input :
temp = []
for x in elem:
if x> 0 :
temp.append(x)
Output.append(temp)
print ("Initial List is :", Input )
print ("Modified list is :", Output)
|
Output:
Initial List is : [[10, -11, 222], [42, -222, -412, 99, -87]]
Modified list is : [[10, 222], [42, 99]]
Time complexity: O(n*m), where n is length of Input list and m is length of sub lists in Input list.
Auxiliary Space: O(k), where k is length of Output list.
Method #2: Using map and list Comprehension
Python3
Input = [[ 10 , - 11 , 222 ], [ 42 , - 222 , - 412 , 99 , - 87 ]]
temp = map ( lambda elem: filter ( lambda a: a> 0 , elem), Input )
Output = [[a for a in elem if a> 0 ] for elem in temp]
print ("Initial List is :", Input )
print ("Modified list is :", Output)
|
Output:
Initial List is : [[10, -11, 222], [42, -222, -412, 99, -87]]
Modified list is : [[10, 222], [42, 99]]
Time complexity: O(n*n), where n is the length of the test_list. The map and list Comprehension takes O(n*n) time
Auxiliary Space: O(n), where n is the number of elements in the input list
Method #3: Using List Comprehension
Python3
Input = [[ 10 , - 11 , 222 ], [ 42 , - 222 , - 412 , 99 , - 87 ]]
Output = [ [b for b in a if b> 0 ] for a in Input ]
print ("Initial List is :", Input )
print ("Modified list is :", Output)
|
Output:
Initial List is : [[10, -11, 222], [42, -222, -412, 99, -87]]
Modified list is : [[10, 222], [42, 99]]
Method #4: Using map, lambda, filter Here is an example of how you can use the map() function, the filter() function, and a lambda function to extract the positive elements from each sublist in a list of lists:
The code first initializes a list test_list with two sublists, each containing a mix of positive and negative integers. Then, it uses the map() function, the filter() function, and a lambda function to extract the positive elements from each sublist.
The map() function applies the lambda function to each element in the list test_list, which is a sublist in this case. The lambda function, in turn, uses the filter() function to filter out all the elements in the sublist that are not greater than 0. This results in a list of sublists, where each sublist contains only the positive elements from the corresponding sublist in the original list. Finally, the list() function is used to convert the map object returned by map() into a list, which is stored in the variable result.
Python3
test_list = [[ 10 , - 11 , 222 ], [ 42 , - 222 , - 412 , 99 , - 87 ]]
result = list ( map ( lambda x: list ( filter ( lambda y: y > 0 , x)), test_list))
print ( "The list after filtering positive elements : " + str (result))
|
Output
The list after filtering positive elements : [[10, 222], [42, 99]]
This approach has a time complexity of O(nm), where n is the number of sublists in the list and m is the number of elements in each sublist, because it requires one iteration over each element in each sublist. The space complexity is also O(nm), because a new list with size equal to the number of elements in the original list is created.
Method #5: Using Regular Expressions
In this approach, we use regular expressions to match positive integers in each sublist. The regular expression pattern \b\d+\b matches one or more digits (\d+) surrounded by word boundaries (\b). This ensures that only whole numbers are matched, and not negative numbers with a minus sign.
Python3
import re
Input = [[ 10 , - 11 , 222 ], [ 42 , - 222 , - 412 , 99 , - 87 ]]
pattern = r '\b\d+\b'
Output = []
for elem in Input :
temp = []
for x in elem:
if re.match(pattern, str (x)):
temp.append( int (x))
Output.append(temp)
print ( "Initial List is :" , Input )
print ( "Modified list is :" , Output)
|
Output
Initial List is : [[10, -11, 222], [42, -222, -412, 99, -87]]
Modified list is : [[10, 222], [42, 99]]
Time complexity: O(n*m), where n is length of Input list and m is length of sub lists in Input list.
Auxiliary Space: O(k), where k is length of Output list.
Similar Reads
Python | Indices list of matching element from other list
Sometimes, while working with Python list, we have a problem in which we have to search for an element in list. But this can be extended to a list and need to find the exact places where elements occur in another list. Let's discuss certain ways in which this task can be performed. Method #1: Using
7 min read
Python | Positions of maximum element in list
Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python | Find common elements in list of lists
The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists. Let's discuss certain ways in which this operation can be
6 min read
Python - Test if elements of list are in Min/Max range from other list
Given two lists, the task is to write a Python Program to return true if all elements from second list are in range of min and max values of the first list. Examples: Input : test_list = [5, 6, 3, 7, 8, 10, 9], range_list = [4, 7, 9, 6] Output : True Explanation : Min and max in list 1 are 3 and 10,
6 min read
Python | Column Product in List of lists
Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Letâs discuss certain ways in which this problem can be solved. Meth
6 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 min read
Python - Smallest integer possible from combination of list elements
Given a list of integers, the task is to get smallest integer possible from the combination of list elements. This is one of the problems that is essential in a competitive point of view and this article discusses various shorthands to solve this problem in Python. Letâs discuss certain ways in whic
4 min read
Python Program to get Maximum product of elements of list in a 2D list
Given a 2D list, we need to find the maximum product of elements for each list within the 2D list and determine which list yields the highest product. The goal is to efficiently compute the product of elements and identify the list with the maximum product. Using loop with prod from mathThis method
4 min read
Python | Product of kth column in List of Lists
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Usin
4 min read
Python - Extracting Priority Elements in Tuple List
Sometimes, while working with Python Records, we can have a problem in which we need to perform extraction of all the priority elements from records, which usually occur as one of the binary element tuple. This kind of problem can have possible application in web development and gaming domains. Let'
5 min read