How to find average value of a list in python
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average =
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
python
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
6
down vote
favorite
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average =
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
python
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
1
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average =
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
python
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average =
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
python
python
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 hours ago


Melvyn Sopacua
1457
1457
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
Chiu Chiu
341
341
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Chiu Chiu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
1
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago
add a comment |Â
1
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
1
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago
1
1
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
1
1
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
2
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be>= 50
, the specs say "which are 50 or more". Otherwise a good answer.
– paxdiablo
2 hours ago
@Daniel: You're right, but raisingZeroDivisionError
is simply wrong.
– CristiFati
1 hour ago
 |Â
show 1 more comment
up vote
4
down vote
In addition to U9-Forward's answer, one using filter
and mean
:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
Nice too :-), thought me__le__
:-)
– U9-Forward
2 hours ago
Again (see comment on U9's answer),le
should probably belt
(or whatever "less than" is).
– paxdiablo
2 hours ago
add a comment |Â
up vote
0
down vote
Hope you are looking for this:
def get_pass_average(marks):
marksAbove =
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
add a comment |Â
up vote
0
down vote
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None
):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
2
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be>= 50
, the specs say "which are 50 or more". Otherwise a good answer.
– paxdiablo
2 hours ago
@Daniel: You're right, but raisingZeroDivisionError
is simply wrong.
– CristiFati
1 hour ago
 |Â
show 1 more comment
up vote
6
down vote
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
2
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be>= 50
, the specs say "which are 50 or more". Otherwise a good answer.
– paxdiablo
2 hours ago
@Daniel: You're right, but raisingZeroDivisionError
is simply wrong.
– CristiFati
1 hour ago
 |Â
show 1 more comment
up vote
6
down vote
up vote
6
down vote
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
edited 1 hour ago
answered 2 hours ago


U9-Forward
5,6852428
5,6852428
2
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be>= 50
, the specs say "which are 50 or more". Otherwise a good answer.
– paxdiablo
2 hours ago
@Daniel: You're right, but raisingZeroDivisionError
is simply wrong.
– CristiFati
1 hour ago
 |Â
show 1 more comment
2
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be>= 50
, the specs say "which are 50 or more". Otherwise a good answer.
– paxdiablo
2 hours ago
@Daniel: You're right, but raisingZeroDivisionError
is simply wrong.
– CristiFati
1 hour ago
2
2
Watch out for empty list!
– CristiFati
2 hours ago
Watch out for empty list!
– CristiFati
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati Done!!!
– U9-Forward
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
@CristiFati: The OP didn't define, how to handle empty lists.
– Daniel
2 hours ago
Just one nitpick, it should be
>= 50
, the specs say "which are 50 or more". Otherwise a good answer.– paxdiablo
2 hours ago
Just one nitpick, it should be
>= 50
, the specs say "which are 50 or more". Otherwise a good answer.– paxdiablo
2 hours ago
@Daniel: You're right, but raising
ZeroDivisionError
is simply wrong.– CristiFati
1 hour ago
@Daniel: You're right, but raising
ZeroDivisionError
is simply wrong.– CristiFati
1 hour ago
 |Â
show 1 more comment
up vote
4
down vote
In addition to U9-Forward's answer, one using filter
and mean
:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
Nice too :-), thought me__le__
:-)
– U9-Forward
2 hours ago
Again (see comment on U9's answer),le
should probably belt
(or whatever "less than" is).
– paxdiablo
2 hours ago
add a comment |Â
up vote
4
down vote
In addition to U9-Forward's answer, one using filter
and mean
:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
Nice too :-), thought me__le__
:-)
– U9-Forward
2 hours ago
Again (see comment on U9's answer),le
should probably belt
(or whatever "less than" is).
– paxdiablo
2 hours ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
In addition to U9-Forward's answer, one using filter
and mean
:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
In addition to U9-Forward's answer, one using filter
and mean
:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
answered 2 hours ago
Daniel
30.8k42555
30.8k42555
Nice too :-), thought me__le__
:-)
– U9-Forward
2 hours ago
Again (see comment on U9's answer),le
should probably belt
(or whatever "less than" is).
– paxdiablo
2 hours ago
add a comment |Â
Nice too :-), thought me__le__
:-)
– U9-Forward
2 hours ago
Again (see comment on U9's answer),le
should probably belt
(or whatever "less than" is).
– paxdiablo
2 hours ago
Nice too :-), thought me
__le__
:-)– U9-Forward
2 hours ago
Nice too :-), thought me
__le__
:-)– U9-Forward
2 hours ago
Again (see comment on U9's answer),
le
should probably be lt
(or whatever "less than" is).– paxdiablo
2 hours ago
Again (see comment on U9's answer),
le
should probably be lt
(or whatever "less than" is).– paxdiablo
2 hours ago
add a comment |Â
up vote
0
down vote
Hope you are looking for this:
def get_pass_average(marks):
marksAbove =
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
add a comment |Â
up vote
0
down vote
Hope you are looking for this:
def get_pass_average(marks):
marksAbove =
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Hope you are looking for this:
def get_pass_average(marks):
marksAbove =
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
Hope you are looking for this:
def get_pass_average(marks):
marksAbove =
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
answered 2 hours ago
Pranab
268129
268129
add a comment |Â
add a comment |Â
up vote
0
down vote
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None
):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
add a comment |Â
up vote
0
down vote
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None
):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None
):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None
):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
answered 2 hours ago


paxdiablo
612k16612171645
612k16612171645
add a comment |Â
add a comment |Â
Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.
Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.
Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.
Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52464451%2fhow-to-find-average-value-of-a-list-in-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Possible duplicate of number of values in a list greater than a certain number
– Bazingaa
1 hour ago
1
It is strange how everyone is providing answers without providing what OP specifically asked Please helps me to figure out the problems in my code,. It would be good to first explain the problem and then provide new solutions
– Bazingaa
1 hour ago