Open In App

Python – Calculate the percentage of positive elements of the list

Last Updated : 08 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, compute the percentage of positive elements in the list.

Input : test_list = [4, 6, -2, 3, -8, -9, -1, 8, 9, 1] 
Output : 60.0 
Explanation : 6/10 elements are positive.

Input : test_list = [-4, 6, -2, 3, -8, -9, -1, 8, 9, 1] 
Output : 50.0 
Explanation : 5/10 elements are positive. 

Method #1 : Using len() + list comprehension

In this, we construct positive elements list using list comprehension and then compute the length of lists using len(), both lengths are divided and multiplied by 100 to get percentage count.

Python3




# Python3 code to demonstrate working of
# Positive values percentage
# Using len() + list comprehension
 
# initializing list
test_list = [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting filtered list using comprehension and
# division to get fraction
res = (len([ele for ele in test_list if ele > 0]) / len(test_list)) * 100
 
# printing result
print("Positive elements percentage : " + str(res))


Output:

The original list is : [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
Positive elements percentage : 60.0

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using filter() + lambda + len()

In this, we perform tasks of getting positive elements using filter() and lambda, rest all tasks are performed similar to the above methods.

Python3




# Python3 code to demonstrate working of
# Positive values percentage
# Using filter() + lambda + len()
 
# initializing list
test_list = [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting filtered list using filter(), lambda and
# division to get fraction
res = (len(list(filter(lambda ele: ele > 0, test_list))) / len(test_list)) * 100
 
# printing result
print("Positive elements percentage : " + str(res))


Output:

The original list is : [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
Positive elements percentage : 60.0

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  filter() + lambda + len() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #3 : Using list(),map(),find()

Initially convert each list element to string and check for – sign in string, if there is no – sign and string is not ‘0’ then elements are positive.Add all those elements to output list

Python3




# Python3 code to demonstrate working of
# Positive values percentage
 
# initializing list
test_list = [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
 
# printing original list
print("The original list is : " + str(test_list))
 
res=[]
x=list(map(str,test_list))
for i in x:
    if(i.find("-")==-1 and i!="0"):
        res.append(int(i))
     
re=(len(res)*100)/len(test_list)
# printing result
print("Positive elements percentage : " + str(re))


Output

The original list is : [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
Positive elements percentage : 60.0

Method #4: Using for loop

Python3




test_list = [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
positive_count = 0
for ele in test_list:
    if ele > 0:
        positive_count += 1
res = (positive_count / len(test_list)) * 100
print("Positive elements percentage : " + str(res))
#This code is contributed by Vinay pinjala.


Output

Positive elements percentage : 60.0

Time complexity:O(n)

Space complexity: O(1)

Method #5:  Using numpy and logical indexing:

Algorithm:

1.Convert the given list to a numpy array.
2.Count the number of positive elements in the numpy array using the expression ‘len(test_array[test_array > 0])’.
3.Calculate the percentage of positive elements in the original list using the formula ‘(positive_count / len(test_list)) * 100’.
4.Print the final result.

Python3




import numpy as np
test_list = [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
# printing original list
print("The original list is : " + str(test_list))
 
test_array = np.array(test_list)
positive_count = len(test_array[test_array > 0])
res = (positive_count / len(test_list)) * 100
print("Positive elements percentage : " + str(res))
 
#This code is contributed by Jyothi pinjala.


Output:

The original list is : [4, 6, -2, 3, -8, 0, -1, 8, 9, 1]
Positive elements percentage : 60.0
Time complexity: O(n)

Converting the list to a numpy array takes O(n) time.
Counting the number of positive elements using numpy array operations takes O(n) time.
The remaining operations take constant time.
Therefore, the overall time complexity is O(n).
Auxiliary Space: O(n)

Converting the list to a numpy array requires O(n) space.
The remaining space usage is constant.
Therefore, the overall space complexity is O(n).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads