question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

list(ndarray) != ndarray.tolist()

See original GitHub issue

I stumbled over this:

import numpy as np
import json

ndarray = np.array([1, 2, 3], dtype=np.float32)

# works:
json.dumps(ndarray.tolist())

# does not work: json.dumps(list(ndarray))

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MartinThomacommented, Jan 30, 2018

Ah, I thought that there might be a special method similar to __iter__(). I guess changing __iter__() is not an option and probably this is what is used by list()?

1reaction
rlabbecommented, Jan 30, 2018

list() is part of Python. It cannot be rewritten to know about every type in existence. I go write the “RogerPy” module, introduce my own types, and list is supposed to know about it? Not possible.

list just iterates over an iterable: ‘return [i for i in x]’

It doesn’t know anything about the underlying type in x, it merely requires that it be iterable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.ndarray.tolist — NumPy v1.24 Manual
numpy.ndarray.tolist#. method. ndarray.tolist()#. Return the array as an a.ndim -levels deep nested list of Python scalars. Return a copy of the array data ......
Read more >
Python | Numpy ndarray.tolist() - GeeksforGeeks
With the help of numpy.ndarray.tolist() method, we can have the list of data elements which is converted from an ndarray using ndarray.tolist() ......
Read more >
How To Convert a NumPy Array to List in Python - DigitalOcean
With NumPy, np.array objects can be converted to a list with the tolist() function. The tolist() function doesn't accept any arguments.
Read more >
How to Convert NumPy Array to List - Spark by {Examples}
How to convert NumPy Array to list in Python? To convert a NumPy array (ndarray) to a Python list use ndarray.tolist() function, this...
Read more >
Convert NumPy array to Python list - Stack Overflow
Use tolist() : >>> import numpy as np >>> np.array([[1,2,3],[4,5,6]]).tolist() [[1, 2, 3], [4, 5, 6]]. Note that this converts the values ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found