# Python NumPy to list with examples

In this Python NumPy tutorial, we will discuss Python NumPy to list and also cover the below examples:

Python numpy list to array
Python numpy list to matrix
Python numpy list to ndarray
Python numpy list to string
Python numpy list to CSV
Python numpy array to list of strings
Python numpy array to list of tuples
Python numpy tuple to list
Python numpy array to list of lists

## Python numpy to list

In this section, we will learn about python numpy to list.
Python stores data in several different ways but the most popular methods are lists and dictionaries.
Arrays also a data structure that is used to store the data.
These arrays are similar to the list.
Both lists and arrays are used for storing data.
The LIst is mutable as well as array is mutable. So we can add and remove an element from the list and array.
Both can be indexed and iterated.
Both can be slicing operations.
List are containers for elements having different datatype but arrays as containers for elements of the same datatype.
We can store int, float, string in a single list but in arrays, we need to store the same type of data.

Syntax:

Here is the Syntax of numpy array to list

numpy.ndarray.tolist()

It returns a copy of the array data as a python list and the possible nested list of array elements.

Example:

import numpy as np

arr1= np.array([2, 4, 5])
list1 = arr1.tolist()
print(list1)

Here is the Screenshot of following given code

Python numpy to list

The above code we can use to convert NumPy to list in Python.

## Python numpy list to array

In this section, we will learn about python numpy list to an array.
In python, the list can be converted to arrays by using two methods from the numpy library.
In this method, we can easily use the function using numpy. array().
First, we import a numpy library and then initialize a list, and converting the list to an array.

Example:

import numpy

list = [2, 4, 8, 9, 2, 5, 6]
arr1 = numpy.array(list)
print(arr1)

Here is the Screenshot of following given code

Python numpy list to array

## Python numpy list to matrix

In this section, we will learn about python’s numpy list to a matrix.
Use the np. array(list) function to convert a list into a matrix form.
The np. array function takes an iterable and returns a numpy array.

Example:

import numpy

list = [2, 4, 8],[9, 2, 5]
arr1 = numpy.array(list)
print(arr1)

Here is the Screenshot of following given code

Python numpy list to matrix

This is how to convert numpy list to matrix in Python.

## Python numpy list to ndarray

In this section, we will learn about python numpy list to ndarray.
ndarray is a (usually fixed-size) multidimensional container of items of the same type and size.
First, we have to import a package and initialize the nested 4-dimensional list, and then use numpy.array() function to convert the list to the array and store it in a different object.
It returns in the matrix form and displays the result in a numpy array.

Example:

import numpy

list = [2, 4, 8,4],[9, 2, 5,4],[1,3,4,5],[3,4,5,6]
arr1 = numpy.array(list)
print(arr1)

Here is the Screenshot of following given code

Python numpy list to ndarray

This is an example of how to covert numpy list to ndarray in Python.

## Python numpy list to string

In this section, we will learn about python numpy list to string.
In this method, we are using the join() function.
First, we have to create a function list to string and initialize an empty string.
And it will return the string.
The list will contain both string and integer as its element.

Example:

import numpy as np
def lToS(list1):

str2 = ” ”

return (str2.join(list1))
list1 = [‘John’, ‘GEORGE’, ‘MICHEAL’]
print(lToS(list1))

Here is the Screenshot of following given code

Python numpy list to string

Another method from numpy list to string by using map() method.
Use map() method for converting elements in the list to string.

Example:

import numpy as np

str1 = [‘Mango’,’Onion’,’Apple’]
listToStr = ‘ ‘.join(map(str, str1))
print(listToStr)

Here is the Screenshot of following given code

Python numpy list to string map method

This is how to convert numpy list to string in Python.

## Python numpy list to CSV

In this section, we will learn about the numpy list to CSV.
The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object.
In this method, we use the dataframe function to store the list in a dataframe and we simply export it to a CSV file using to_csv.

Example:

import numpy as np
import pandas as pd

list = [2,3,4,5,6,7,8]
print(list)
DF = pd.DataFrame(list)
DF.to_csv(‘/home/arvind/Documents/test.csv’)

Here is the Screenshot of following given code

Python numpy list to csv
numpy list to csv

## Python numpy array to list of strings

In this section, we will learn about the numpy array to list strings.
In this method, we can easily use the function to list().it converts the array into a list.
In the list, we can only use the string characters.
It returns a copy of the array data as a Python list.

Example:

import numpy as np

a = np.array([[‘john’,’George’, ‘Micheal’], [‘orange’, ‘apple’, ‘grapes’]])
list1 = a.tolist()
print(list1)

Here is the Screenshot of following given code

Python numpy array to list of strings

This is how to convert numpy array to list of strings in Python.

## Python numpy array to list of tuples

In this section, we will learn about the numpy array to list tuples.
In this method, we can easily use map and tuple functions.
Map function returns a map object of the results after applying the given function to each item of a given iterable.
A tuple is an object in Python that has items separated by commas and enclosed in round brackets.

Example:

import numpy as np

arr = np.array([[‘mango’, ‘apple’], [‘grapes’, ‘orange’]])
res = tuple(map(tuple, arr))
print(res)

Here is the Screenshot of following given code

Python numpy array to list of tuples

## Python numpy tuple to list

In this section, we will learn about the numpy tuple to list
Tuple cannot be changed but in list we can add new values and elements.
Tuple uses parenthesis whereas list uses square brackets.
Python list method list() takes sequence types and converts them to lists. This is used to convert a given tuple into list.
The output will be in the form of list.

Example:

import numpy as np
Tup = (456, ‘tuv’, ‘mno’, ‘klm’)
List1 = list(Tup)
print(List1)

In the above example first we create a tuple and then use the function list() and converts them into the list.

Here is the Screenshot of following given code

Python numpy tuple to list

This is how to convert numpy array to list of tuples in Python.

## Python numpy array to list of lists

In this section, we will learn about the numpy array to list of lists.
In Python’s numpy module, the ndarray class provides a member function tolist(), which returns a list containing the copy of elements in the numpy array. If the numpy array is 2D, then it returns a list of lists.
List are containers for elements having different datatype but arrays as containers for elements of the same datatype.
The output will be in the form of lists into the list.

Example:

import numpy as np

arr1 = np.array([[4, 5, 6, 7],
[5, 4, 4, 9],
[3, 4, 5, 6]])

listoflists = arr1.tolist()
print(listoflists)

Here is the Screenshot of following given code

Python numpy array to list of lists

You may like the following Python tutorials:

In this Python numpy tutorial, we will discuss Python NumPy to list and also cover the below examples:

Python numpy list to array
Python numpy list to matrix
Python numpy list to ndarray
Python numpy list to string
Python numpy list to CSV
Python numpy array to list of strings
Python numpy array to list of tuples
Python numpy tuple to list
Python numpy array to list of lists 