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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:54:39+00:00 2026-06-07T23:54:39+00:00

I noticed this problem when a computer running Ubuntu was updated recently and the

  • 0

I noticed this problem when a computer running Ubuntu was updated recently and the default version of Python changed to 2.7.

import json
import numpy as np

json.dumps(list(np.arange(5))) # Fails, throws a "TypeError: 0 is not JSON serializable"
json.dumps(np.arange(5).tolist()) # Works 

Is there a difference between list() and the tolist() methd of a numpy array?

  • 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-07T23:54:41+00:00Added an answer on June 7, 2026 at 11:54 pm

    It looks like the tolist() method turns the numpy int32 (or whatever size you have) back into an int, which JSON knows what to do with:

    >>> list(np.arange(5))
    [0, 1, 2, 3, 4]
    >>> type(list(np.arange(5)))
    <type 'list'>
    >>> type(list(np.arange(5))[0])
    <type 'numpy.int32'>
    >>> np.arange(5).tolist()
    [0, 1, 2, 3, 4]
    >>> type(np.arange(5).tolist())
    <type 'list'>
    >>> type(np.arange(5).tolist()[0])
    <type 'int'>
    

    As the docs say for tolist():

    Return the array as a (possibly nested) list.

    Return a copy of the array data as a (nested) Python list. Data items
    are converted to the nearest compatible Python type.

    The last line makes the difference here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently updated my python installation to 2.7 (previously 2.5), and I've noticed a
Possible Duplicate: Foursquare API for venue user image error I first noticed this problem
I noticed some similar questions about this problem when I typed the title, but
I don't know if anyone else even has this problem, or has noticed, but
I noticed people wrote about this circular reference problem quite a bit before but
I notice this thread: Fastish Python/Jython IPC , and I have a similar problem,
I just recently noticed this behaviour. To put it shortly, it displays the commits
It is possible that this problem doesn't display on your computer. A working example
Noticed this today when a patch was submitted with the following line: lblCompletionTime.Text =
I noticed This question , but my question is a bit more specific. Is

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.