{"id":4662,"date":"2023-10-25T11:21:31","date_gmt":"2023-10-25T05:51:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=4662"},"modified":"2023-11-10T11:58:36","modified_gmt":"2023-11-10T06:28:36","slug":"create-numpy-array-from-list-tuple-or-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/create-numpy-array-from-list-tuple-or-list-of-lists-in-python\/","title":{"rendered":"Create NumPy Array from List, Tuple or List of Lists in Python"},"content":{"rendered":"

Methods to create NumPy array from list, tuple, or list of lists in Python<\/h2>\n

In this article, we will discuss some basics of NumPy array and how to create NumPy array from lists, tuples, and Lists of lists in python.<\/p>\n

NumPy<\/h3>\n

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.<\/p>\n

numpy.array()<\/h3>\n

This method is going to be widely used in our program so it will be beneficial to study this method in advance so that it will be easy for us to understand the concepts. This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.<\/p>\n

Syntax: numpy.<\/span>array<\/span>(<\/span>object, dtype=<\/span>None<\/span>, copy=<\/span>True<\/span>, order=<\/span>‘K’<\/span>, subok=<\/span>False<\/span>, ndmin=<\/span>0<\/span>)<\/span><\/p>\n

This method returns a numpy array.<\/p>\n

numpy.asarray()<\/h3>\n

This method helps us to create a numpy array from already defined data structures in python like lists, tuples, and nested lists.<\/p>\n

Syntax: numpy.asarray(arr, dtype=None, order=None)<\/p>\n

Difference between numpy.array() and numpy.asarray()<\/h3>\n

The major difference between both the methods is that numpy.array() will make a duplicate of original copy while the numpy.asarray() make changes in the original copy.<\/p>\n

Create a numpy array from a list<\/h3>\n