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 - Negative index of Element in List We are given a list we need to find the negative index of that element. For example, we are having a list li = [10, 20, 30, 40, 50] and the given element is 30 we need to fin the negative index of it so that given output should be -3.Using index()index() method in Python searches for the first occur
3 min read
Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e
3 min read
Remove Negative Elements in List-Python The task of removing negative elements from a list in Python involves filtering out all values that are less than zero, leaving only non-negative numbers. Given a list of integers, the goal is to iterate through the elements, check for positivity and construct a new list containing only positive num
3 min read
Python | Reverse sign of each element in given list Given a list of integers, write a Python program to reverse the sign of each element in given list. Examples: Input : [-1, 2, 3, -4, 5, -6, -7] Output : [1, -2, -3, 4, -5, 6, 7] Input : [-5, 9, -23, -2, 7] Output : [5, -9, 23, 2, -7] Methods #1: List comprehension Python3 # Python3 program to Conver
3 min read
Get the indices of all occurrences of an element in a list - Python We are given a list and our task is to find all the indices where a particular element occurs. For example, if we have a list like [1, 2, 3, 2, 4, 2] and the element is 2, then the output will be [1, 3, 5] because 2 appears at these positions.Using List ComprehensionList comprehension allows for a c
2 min read