Skip to content
Laviyx Learn

Try

Python Lab · Experiment 10

Convert a list and a tuple into arrays

PythonWeek 3JNTUH R22 · CS206ES

Aim

To write a Python program that converts a list and a tuple into NumPy arrays, and shows that the results are arrays by printing their type, shape and the result of an arithmetic operation on them.

Algorithm

  1. Import the NumPy module as np.
  2. Create a list of numbers and a tuple of numbers.
  3. Convert each of them to an array with np.array().
  4. Print the original list and tuple, the arrays, and the type of each.
  5. Print the shape of each array.
  6. Multiply the list and the array by 2 to show how differently lists and arrays behave.
  7. Convert a nested list into a two-dimensional array and print it.

Program

Python
import numpy as np

marks_list = [72, 85, 90, 64]
prices_tuple = (12.5, 40.0, 7.25)

# Convert the list and the tuple into NumPy arrays
marks_array = np.array(marks_list)
prices_array = np.array(prices_tuple)

print("List            :", marks_list, type(marks_list))
print("Array from list :", marks_array, type(marks_array))
print("Tuple           :", prices_tuple, type(prices_tuple))
print("Array from tuple:", prices_array, type(prices_array))
print()

print("Shape of marks array :", marks_array.shape)
print("Shape of prices array:", prices_array.shape)
print()

# Arrays work element by element; lists do not
print("marks_list * 2  =", marks_list * 2)
print("marks_array * 2 =", marks_array * 2)
print()

# A list of lists becomes a two-dimensional array
table = np.array([[1, 2, 3], [4, 5, 6]])
print("2-D array from a nested list:")
print(table)
print("Shape:", table.shape)

Output

Output
List            : [72, 85, 90, 64] <class 'list'>
Array from list : [72 85 90 64] <class 'numpy.ndarray'>
Tuple           : (12.5, 40.0, 7.25) <class 'tuple'>
Array from tuple: [12.5  40.    7.25] <class 'numpy.ndarray'>

Shape of marks array : (4,)
Shape of prices array: (3,)

marks_list * 2  = [72, 85, 90, 64, 72, 85, 90, 64]
marks_array * 2 = [144 170 180 128]

2-D array from a nested list:
[[1 2 3]
 [4 5 6]]
Shape: (2, 3)

Recorded by running this exact program.

How it works

np.array() takes any sequence (a list, a tuple, or a list of lists) and builds a new array from it. np.asarray() does the same, but if the input is already an array it returns it without copying.

All items of a NumPy array have one type. If you mix integers and floats, everything becomes float; if you mix numbers and strings, everything becomes a string.

Python also has a built-in array module, which makes one-dimensional arrays of a single numeric type: array.array("i", [1, 2, 3]) for integers or array.array("d", (1.5, 2.5)) for floats. It has no element-by-element arithmetic, so NumPy arrays are what people usually mean by arrays in Python.

NumPy is not part of the standard library. Install it with pip install numpy if import numpy fails.

Common mistakes

  • Calling np.array(1, 2, 3) with separate values. np.array takes one sequence, so write np.array([1, 2, 3]).

  • Expecting list * 2 to double every value. For a list it repeats the list; only a NumPy array multiplies each element.

  • Forgetting import numpy as np and getting NameError: name 'np' is not defined.

  • Making a 2-D array from rows of different lengths. NumPy needs every row to have the same number of items.

Viva questions

Try to answer each one aloud before you open it.

  • What is the difference between a list and a NumPy array?

    A list can hold items of different types and has no element-wise arithmetic. A NumPy array holds items of one type, stored compactly, and operations such as * 2 apply to every element. Arrays are much faster for numerical work.

  • How do you convert a list or a tuple into an array?

    Pass it to np.array(), for example np.array([1, 2, 3]) or np.array((1, 2, 3)).

  • What does the shape attribute of an array give?

    A tuple with the size of each dimension. A 1-D array of 4 items has shape (4,); a 2-row, 3-column array has shape (2, 3).

  • What is the array module in Python?

    A standard-library module whose array.array stores a one-dimensional sequence of a single numeric type, given by a type code such as 'i' for int or 'd' for float.

Learn the concepts

All Python Programming Lab experiments →