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

  • Home
  • SEARCH
  • 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 8568623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:09:11+00:00 2026-06-11T18:09:11+00:00

I have seen some python functions that work generally receiving a (n,2) shaped numpy

  • 0

I have seen some python functions that work generally receiving a (n,2) shaped numpy ndarray as argument, but can also “automagically” receive (2,n) or even len(2) sequences (tuple or list).

How is it pythonically achieved? Is there a unified good practice to check and treat these cases (example, functions in numpy and scipy module), or each developer implements which he thinks best?

I’d just like to avoid chains of (possibly nested) chains of ifs/elifs, in case there is a well known better way.

Thanks for any help.

  • 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-11T18:09:13+00:00Added an answer on June 11, 2026 at 6:09 pm

    You can use the numpy.asarray function to convert any sequence-like input to an array:

    >>> import numpy
    >>> numpy.asarray([1,2,3])
    array([1, 2, 3])
    >>> numpy.asarray(numpy.array([2,3]))
    array([2, 3])
    >>> numpy.asarray(1)
    array(1)
    >>> numpy.asarray((2,3))
    array([2, 3])
    >>> numpy.asarray({1:3,2:4})
    array({1: 3, 2: 4}, dtype=object)
    

    It’s important to note that as the documentation says No copy is performed if the input is already an ndarray. This is really nice since you can pass an existing array in and it just returns the same array.

    Once you convert it to a numpy array, just check the length if that’s a requirement. Something like:

    >>> def f(x):
    ...    x = numpy.asarray(x)
    ...    if len(x) != 2:
    ...       raise Exception("invalid argument")
    ... 
    >>> f([1,2])
    >>> f([1,2,3])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 4, in f
    Exception: invalid argument
    

    Update:
    Since you asked, here’s a “magic” function that will except *args as an array also:

    >>> def f(*args):
    ...    args = numpy.asarray(args[0]) if len(args) == 1 else numpy.asarray(args)
    ...    return args
    ... 
    >>> f(7,3,5)
    array([7, 3, 5])
    >>> f([1,2,3])
    array([1, 2, 3])
    >>> f((2,3,4))
    array([2, 3, 4])
    >>> f(numpy.array([1,2,3]))
    array([1, 2, 3])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just a simple question. Some python functions I have seen are called like this,
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
I have seen some related questions but none focusing on the specific problem I
i have seen some hand rolled solutions but does jquery out of the box
I have seen some of the other answers on this topic but dont really
I have seen some demos about the jQuery wizard plugin on websites. But all
I'm trying to test some python code that uses urllib2 and lxml. I've seen
I have seen some code in Pinax and other django apps that instead of
I have seen some mention of some form of TDD for Python with Google
In Python I have seen many examples where multiprocessing is called but the target

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.