Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Get positive elements from given list of lists

Improve Article
Save Article
  • Last Updated : 07 Jan, 2023
Improve Article
Save Article

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]]

  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]]

  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


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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!