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 8736755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:22:07+00:00 2026-06-13T10:22:07+00:00

I have a numpy object array containing several lists of index numbers: >>> idxLsts

  • 0

I have a numpy object array containing several lists of index numbers:

>>> idxLsts = np.array([[1], [0, 2]], dtype=object)

I define a vectorized function to append a value to each list:

>>> idx = 99  
>>> f = np.vectorize(lambda idxLst: idxLst.append(idx))

I invoke the function. I don’t care about the return value, just the side effect.

>>> f(idxLsts)  
array([None, None], dtype=object)

The index 99 was added twice to the first list. Why? I’m stumped.

>>> idxLsts
array([[1, 99, 99], [0, 2, 99]], dtype=object)

With other values of idxLsts, it doesn’t happen:

>>> idxLsts = np.array([[1, 2], [0, 2, 4]], dtype=object)
>>> f(idxLsts)
array([None, None], dtype=object)
>>> idxLsts
array([[1, 2, 99], [0, 2, 4, 99]], dtype=object)

My suspicion is it’s related to the documentation which says: “Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a numpy array as output. The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy.”

  • 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-13T10:22:08+00:00Added an answer on June 13, 2026 at 10:22 am

    From the vectorize docstring:

    The data type of the output of `vectorized` is determined by calling
    the function with the first element of the input.  This can be avoided
    by specifying the `otypes` argument.
    

    And from the code:

            theout = self.thefunc(*newargs)
    

    This is an extra call to thefunc, used to determine the output type. This is why the first element is getting two 99s appended.

    This behavior happens in your second case as well:

    import numpy as np
    idxLsts = np.array([[1, 2], [0,2,4]], dtype = object)
    idx = 99
    f = np.vectorize(lambda x: x.append(idx))
    f(idxLsts)
    print(idxLsts)
    

    yields

    [[1, 2, 99, 99] [0, 2, 4, 99]]
    

    You could use np.frompyfunc instead of np.vectorize:

    import numpy as np
    idxLsts = np.array([[1, 2], [0,2,4]], dtype = object)
    idx = 99
    f = np.frompyfunc(lambda x: x.append(idx), 1, 1)
    f(idxLsts)
    print(idxLsts)
    

    yields

    [[1, 2, 99] [0, 2, 4, 99]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have numpy array: >>> data dtype([('date', '|O4'), ('value', '<f8')] where date object is
I am creating an object in python. I have a numpy array from an
I have a numpy array which came from a cv2.imread and so has dtype
Suppose I have a large in memory numpy array, I have a function func
I have an numpy array which I save to a image using savefig(). Then
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
I have two numpy arrays that define the x and y axes of a
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a numpy array with positive and negative values in. a = array([1,1,-1,-2,-3,4,5])
I have a numpy array like this a = np.array(1) Now if I want

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.