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
# Python code to get positive
# element from list of list
# List Initialisation
Input = [[10, -11, 222], [42, -222, -412, 99, -87]]
Output = []
# Using iteration
for elem in Input:
temp = []
for x in elem:
if x>0:
temp.append(x)
Output.append(temp)
# printing output
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
# Python code to get positive element
# from list of list
# List Initialisation
Input = [[10, -11, 222], [42, -222, -412, 99, -87]]
# Using list comprehension and map
temp = map(lambda elem: filter(lambda a: a>0, elem), Input)
Output = [[a for a in elem if a>0] for elem in temp]
# printing output
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
# Python code to get positive element
# from list of list
# List Initialisation
Input = [[10, -11, 222], [42, -222, -412, 99, -87]]
# Using list comprehension
Output = [ [b for b in a if b>0] for a in Input]
# printing output
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
# initializing list
test_list = [[10, -11, 222], [42, -222, -412, 99, -87]]
# extract positive elements from each sublist using map(), filter(), and lambda
result = list(map(lambda x: list(filter(lambda y: y > 0, x)), test_list))
# print result
print("The list after filtering positive elements : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe 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
# List initialization
Input = [[10, -11, 222], [42, -222, -412, 99, -87]]
# Regular expression pattern to match positive numbers
pattern = r'\b\d+\b'
# Using regular expression
Output = []
for elem in Input:
temp = []
for x in elem:
if re.match(pattern, str(x)):
temp.append(int(x))
Output.append(temp)
# printing output
print("Initial List is :", Input)
print("Modified list is :", Output)
OutputInitial 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 | Remove given element from list of lists
The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 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 | Check if element exists in list of lists
Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. Python3 # Python code to demons
5 min read
Python | Find missing elements in List
Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
7 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 | Remove trailing empty elements from given list
When working with lists in Python, it's common to encounter lists with extra None elements at the end. These trailing None values can cause issues in our programs . We can remove these trailing None elements using simple methods like slicing, loops, or filter.Using List Slicing Slicing is a very eff
3 min read
Python program to find N largest elements from a list
Given a list of integers, the task is to find N largest elements assuming size of list is greater than or equal o N. Examples : Input : [4, 5, 1, 2, 9] N = 2 Output : [9, 5] Input : [81, 52, 45, 10, 3, 2, 96] N = 3 Output : [81, 96, 52] A simple solution traverse the given list N times. In every tra
5 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
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 c
4 min read