How to find average value of a list in python

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question









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














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.










share|improve this question









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












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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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












  • 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












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





share|improve this answer


















  • 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 raising ZeroDivisionError is simply wrong.
    – CristiFati
    1 hour ago

















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)





share|improve this answer




















  • 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


















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()





share|improve this answer



























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





    share|improve this answer




















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      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






























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





      share|improve this answer


















      • 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 raising ZeroDivisionError is simply wrong.
        – CristiFati
        1 hour ago














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





      share|improve this answer


















      • 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 raising ZeroDivisionError is simply wrong.
        – CristiFati
        1 hour ago












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





      share|improve this answer














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






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 raising ZeroDivisionError is simply wrong.
        – CristiFati
        1 hour ago












      • 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 raising ZeroDivisionError 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












      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)





      share|improve this answer




















      • 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















      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)





      share|improve this answer




















      • 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













      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)





      share|improve this answer












      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)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      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 be lt (or whatever "less than" is).
        – paxdiablo
        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
















      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











      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()





      share|improve this answer
























        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()





        share|improve this answer






















          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()





          share|improve this answer












          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()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Pranab

          268129




          268129




















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





              share|improve this answer
























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





                share|improve this answer






















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





                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  paxdiablo

                  612k16612171645




                  612k16612171645




















                      Chiu Chiu is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      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.













                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery