Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:16:07+00:00 2026-06-11T20:16:07+00:00

I am new in Python so please bear with my naive question. I want

  • 0

I am new in Python so please bear with my naive question.

I want to write a function which takes a vector of numbers and computes their average value. So I write a little function as

def my_mean(*args):
    if len(args) == 0:
        return None
    else:
        total = sum(args)
        ave = 1.0 * total / len(args)
        return ave

my_mean(1, 2, 3)
2.0

But this function won’t work if the argument is a list of numbers. For example,

my_mean([1, 2, 3])
Traceback (most recent call last):
  File "/usr/lib/wingide-101-4.1/src/debug/tserver/_sandbox.py", line 1, in <module>
    # Used internally for debug sandbox under external interpreter
  File "/usr/lib/wingide-101-4.1/src/debug/tserver/_sandbox.py", line 21, in my_mean
TypeError: unsupported operand type(s) for +: 'int' and 'list'

I know NumPy has a function numpy.mean which takes a list as argument but not a vector of numbers as my_mean does.

I am wondering if there is a way to make my_mean work in both cases? So:

my_mean(1, 2, 3)
2.0
my_mean([1, 2, 3])
2.0

just like min or max function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:16:08+00:00Added an answer on June 11, 2026 at 8:16 pm

    You can pass in your list by using the *arg syntax:

    my_mean(*[1, 2, 3])
    

    Alternatively, you could detect if your first argument passed in is a sequence and use that instead of the whole args tuple:

    import collections
    
    def my_mean(*args):
        if not args:
            return None
        if len(args) == 1 and isinstance(args[0], collections.Container):
            args = args[0]
        total = sum(args)
        ave = 1.0 * total / len(args)
        return ave
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Python programming, so please bear with my nascent question. If
I am a very new in Python so please forgive me the basic question.
I am new to Python so please don't flame me if the question is
I am new to PYTHON and trying to write script which replaces text in
I am very new to python so please bear with me. I have a
I'm new to python, so please excuse what is probably a pretty dumb question.
I am new to the python language, so please bear with me. Also English
I am new to SO and Python/Django, so please bear with me. In my
I am very new to programming and Python so please bear with me. I
I'm an old-fashioned Pascal programmer, new to OOP and Python, so please bear with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.