Appending Values to an Array

Tags: Array, NumPy

Write a NumPy program to append values to the end of an array. Return the updated array.

Example 1: Input: np.array([1, 2, 3, 4, 5]), np.array([6, 7, 8, 9, 10]) Output: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Example 2: Input: np.array([10, 20, 30, 40, 50]), np.array([60, 70, 80, 90, 100]) Output: array([ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])

You can use np.append() function to append values to the end of an array.

import numpy as np

def append_values(array1, array2):
    return np.append(array1, array2)

print(append_values(np.array([1, 2, 3, 4, 5]), np.array([6, 7, 8, 9, 10])))
print(append_values(np.array([10, 20, 30, 40, 50]), np.array([60, 70, 80, 90, 100])))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!