Open In App

Python | Get positive elements from given list of lists

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 




# 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 




# 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 




# 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.




# 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

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.




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)

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.


Article Tags :