Menu
Coddy logo textTech

Array Constraints

Lesson 3 of 18 in Coddy's Numpy Fundamentals course.

Arrays are fixed-sized. After creating an array with n elements, the size of the array will be n and it cannot be changed later. To have a bigger or smaller array we need to create a new one and populate it with data. Arrays don't support append nor delete like Python lists do.

For example:

ary = np.array([34, 59, 5])

The ary variable holds a 1-dimensional array that contains 3 elements.

To access each element we use [] brackets just like a regular Python list:

ary[1] # holds the value 59

To delete an element use np.delete. it won't affect the original ary, but rather it will return a new array without the element it is deleting.
To replace the new smaller array with the original ary assign the result to the ary variable:

ary = np.delete(ary, 1)

To add an element use np.append

ary = np.append(ary, 5)

Now the ary will hold [34, 59, 5, 5]. np.append also returns a new array and does not modify the original array.

To access the last element we can use multiple options:

ary[-1]
ary[len(ary)-1]
ary[2]

Just like with a normal Python list

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a function named array_modifier that receives 3 values: a list, a value, and an index.

Convert the list to a numpy array, and insert the new value in the specified index. You are not allowed to use list functionalities.

This problem can easily be solved with the builtin .insert, but I want you to practice other functionalities.

Try it yourself

import numpy as np
def array_modifier(lst, value, index):
    pass

All lessons in Numpy Fundamentals