Add a value to each element of an array in Python

In this article, we will learn how to add a number to each element of a NumPy Array in Python.

Given a NumPy Array, we need to Add number to each element of NumPy Array.

Example:             

Given array = [1, 2, 3, 4, 5]
After adding 10 to each value of array: [11 12 13 14 15]

There are multiple ways to add number to each element of NumPy Array. Let’s discuss all the methods one by one with a proper approach and a working code example.

Adding the number directly to the array using + operator

If we directly add a number to the NumPy Array object, then it will be added to each element of the array. Detailed steps are as follows,

  • Import numpy library and create a numpy array
  • Now add the number to array using the plus operator.
  • This will return a new array contains the elements from original numpy array and given number added to each of them.
  • Print the array

Source code

Advertisements
import numpy as np

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
arr=arr + 10

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

The original array :=  [1 2 3 4 5]
The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using vectorized function

The numpy module has a vectorize class. The vectorize class takes a python function as argument and returns a vectorized function. This vectorized function accepts a numpy array as argument and applies that initial function on each element of the array. Then returns a numpy array containing the values returned by the applied function.

Syntax of vectorize

numpy.vectorize(pyfunc)

Parameters:

pyfunc          = Python function or method.

Return:

Returns a vectorized function.    

Approach

  • Import NumPy Library and create a NumPy Array.
  • Create a function that takes a number as parameter and returns it after adding 10 in it.
  • Pass this function to the vectorize class, It returns a vectorized function
  • Pass the NumPy Array to the vectorized function, It will return an array with each elements added with the given number.
  • Print the array

Source code

import numpy as np

def add(num):
    return num + 10

# Creating a NumPy Array
arr = np.array([1, 2, 3, 4, 5])

# Printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
addTen = np.vectorize(add)
arr = addTen(arr)

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using map()

The python map() function will take an iterator and a function as input and returns the iterator by applying the function to the each element of the iterator;

Syntax of map()

map(function, iterator)

Parameters:

function          = Python function or method.
iterator          = List, set, tuple.

Returns:

Returns an iterator.    

Approach

  • Import numpy library and create a numpy array
  • Create a function to add a number to the functional parameter.
  • Pass this function and the array to the map, It will return an list by applying function to each element of iterator
  • Convert it into array and print it.

Source code

import numpy as np

def add(num):
    return num+10

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array : " , arr)

# add 10 to every element of NumPy array 
arr = np.array(list(map(add, arr)))

# printing the array after adding given number
print(" The array after adding given number : " , arr)

OUTPUT:

The original array :  [1 2 3 4 5]
The array after adding given number :  [11 12 13 14 15]

Add a value to each element of an array using for Loop

Use the for loop to iterate over the array and add the given number to each element of the numpy array.

Approach

  • Import numpy library and create a numpy array
  • Using a for loop and range() method iterate the array.
  • Add the given number to the each element
  • Print the array

Source code

import numpy as np

def add(num):
    return num+10
# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
for i in range(0,len(arr)):
    arr[i]=arr[i]+10

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Add a value to each element of an array using List Comprehension

Use the List Comprehension to iterate over the array and apply the add a value to each element of the numpy array.

Approach

  • Import numpy library and create a numpy array
  • Use List Comprehension to iterate over the array and apply the add function
  • It will return a list, Convert that list into a numpy array and print it.

Source code

import numpy as np

def add(num):
    return num+10

# creating  numpy array
arr = np.array([1, 2, 3, 4, 5])

# printing the original array
print(" The original array := " , arr)

# add 10 to every element of NumPy array 
arr = np.array([add(num) for num in arr])

# printing the array after adding given number
print(" The array after adding given number := " , arr)

OUTPUT:

 The original array :=  [1 2 3 4 5]
 The array after adding given number :=  [11 12 13 14 15]

Summary

Great! you made it, We have discussed all possible methods to add a number to each element of NumPy Array in Python. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top