# write a numpy program to convert a list and tuple into arrays

### Mohammed

Guys, does anyone know the answer?

get write a numpy program to convert a list and tuple into arrays from screen.

## NumPy: Convert a list and tuple into arrays

NumPy Array Object Exercises, Practice and Solution: Write a NumPy program to convert a list and tuple into arrays.

## NumPy: Convert a list and tuple into arrays

Last update on August 19 2022 21:50:48 (UTC/GMT +8 hours)

## NumPy: Array Object Exercise-11 with Solution

Write a NumPy program to convert a list and tuple into arrays.

**Sample Solution**:-

**NumPy Code:**

import numpy as np

my_list = [1, 2, 3, 4, 5, 6, 7, 8]

print("List to array: ")

print(np.asarray(my_list))

my_tuple = ([8, 4, 6], [1, 2, 3])

print("Tuple to array: ")

print(np.asarray(my_tuple))

Sample Output: List to array: [1 2 3 4 5 6 7 8] Tuple to array: [[8 4 6] [1 2 3]]

**Python-Numpy Code Editor:**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:**Write a NumPy program to create a 8x8 matrix and fill it with a checkerboard pattern.

**Next:**Write a NumPy program to append values to the end of an array.

## How to convert a list and tuple into NumPy arrays?

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

## How to convert a list and tuple into NumPy arrays?

Difficulty Level : Medium

Last Updated : 29 Aug, 2020

Read Discuss Courses Practice Video

In this article, let’s discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same. Let’s discuss them

**Method 1:**Using numpy.asarray()

It converts the input to an array. The input may be lists of tuples, tuples, tuples of tuples, tuples of lists and ndarray.

**Syntax:**

numpy.asarray( a, type = None, order = None )

**Example:**

## Python3

import numpy as np # list

list1 = [3, 4, 5, 6]

print(type(list1)) print(list1) print() # conversion

array1 = np.asarray(list1)

print(type(array1)) print(array1) print() # tuple

tuple1 = ([8, 4, 6], [1, 2, 3])

print(type(tuple1)) print(tuple1) print() # conversion

array2 = np.asarray(tuple1)

print(type(array2)) print(array2)

**Output:**

[3, 4, 5, 6] [3 4 5 6]

([8, 4, 6], [1, 2, 3])

[[8 4 6] [1 2 3]]

**Method 2:**Using numpy.array()

It creates an array.

**Syntax:**numpy.array( object, dtype = None, *, copy = True, order = ‘K’, subok = False, ndmin = 0 )

**Parameters:**

**object:**array-like

**dtype:**data-type, optional ( The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. )

**copy:**bool, optional ( If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.). )

**order:**{‘K’, ‘A’, ‘C’, ‘F’}, optional ( same as above )

**subok:**bool, optional ( If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array (default). )

**ndmin:**int, optional ( Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement. )

**Returns:**ndarray ( An array object satisfying the specified requirements. )

**Example:**

## Python3

import numpy as np # list list1 = [1, 2, 3] print(type(list1)) print(list1) print() # conversion

array1 = np.array(list1)

print(type(array1)) print(array1) print() # tuple

tuple1 = ((1, 2, 3))

print(type(tuple1)) print(tuple1) print() # conversion

array2 = np.array(tuple1)

print(type(array2)) print(array2) print()

# list, array and tuple

array3 = np.array([tuple1, list1, array2])

print(type(array3)) print(array3)

**Output:**

[1, 2, 3]

[1 2 3]

(1, 2, 3)

[1 2 3]

[[1 2 3]

[1 2 3]

[1 2 3]]

## Convert Python List to NumPy Arrays

Convert Python List to NumPy Arrays with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.

## Convert Python List to NumPy Arrays

Convert Python List to NumPy Arrays Introduction

In Python, a list is a linear data structure that may store heterogeneous elements. It does not need to be defined and can shrink and expand as needed. On the other end, a NumPy array is a data structure that may store homogenous elements. It is implemented in Python using the NumPy library. This library is very efficient in handling multi-dimensional arrays. It is also very efficient in handling a huge number of data elements. NumPy arrays use less memory than List data structures. Both the NumPy array and the list can be identified by their index value.

**The NumPy library provides two methods for converting lists to arrays in Python.**

Using numpy.array()

Using numpy.asarray()

### Method 1: Using numpy.array()

In Python, the simplest way to convert a list to a NumPy array is with numpy.array() function. It takes an argument and returns a NumPy array. It creates a new copy in memory.

**Program 1**

# importing library of the array in python

import numpy

# initilizing elements of the list

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# converting elements of the list into array elements

arr = numpy.array(a)

# displaying elements of the list

print ("List: ", a)

# displaying elements of the array

print ("Array: ", arr)

**Output:**

List: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Array: [1 2 3 4 5 6 7 8 9]

### Method 2: Using numpy.asarray()

In Python, the second method is numpy.asarray() function that converts a list to a NumPy array. It takes an argument and converts it to the NumPy array. It does not create a new copy in memory. In this, all changes made to the original array are reflected on the NumPy array.

**Program 2**

# importing library of the array in python

import numpy

# initilizing elements of the list

a = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# converting elements of the list into array elements

arr = numpy.asarray(a)

# displaying elements of the list

print ("List:", a)

# displaying elements of the array

print ("Array: ", arr)

**Output:**

List: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Array: [1 2 3 4 5 6 7 8 9]

**Program 3**

# importing library of the NumPy array in python

import numpy

# initilizing elements of the list

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# converting elements of the list into array elements

arr = numpy.asarray(lst)

# displaying elements of the list

print ("List:", lst)

# displaying elements of the array

print ("arr: ", arr)

# made another array out of arr using asarray function

arr1 = numpy.asarray(arr)

#displaying elements of the arr1 before the changes made

print("arr1: " , arr1)

#change made in arr1

arr1[3] = 23

#displaying arr1 , arr , list after the change has been made

print("lst: " , lst)

print("arr: " , arr)

print("arr1: " , arr1)

**Output:**

List: [1, 2, 3, 4, 5, 6, 7, 8, 9]

arr: [1 2 3 4 5 6 7 8 9]

arr1: [1 2 3 4 5 6 7 8 9]

lst: [1, 2, 3, 4, 5, 6, 7, 8, 9]

arr: [ 1 2 3 23 5 6 7 8 9]

arr1: [ 1 2 3 23 5 6 7 8 9]

← Prev Next →

Guys, does anyone know the answer?