Adding K to each element in a Python list of integers



In this article, we will learn how to add a constant K value to each element in a Python list of integers. A list is a data type in Python that stores a sequence of items separated by commas, like this ?

List = [item1, item2, item3?]

Suppose we have a list of integers called "a" and a constant value "k." We need to add this "k" to each item in the "a" list. For example ?

Input:
a = [5, 10, 15, 20] 
k = 5 

Output: #On adding 5 to each element of the list
result = [10, 15, 20, 25]

Adding K to Each List Element in Python

We can add a constant K value to each element in a Python list using the following approaches ?

  • Using a "+" Operator
  • Using List Comprehension and "+" Operator
  • Using the map() function and "+" Operator
  • Using a map() function and add() methods
  • Using the Numpy Library

Let us go through all these one by one ?

Using a "+" Operator

The "+" operator performs addition in Python when used between two integers. We can use it to add a constant k value to each integer in a list by looping through it with a for loop.

Note ? We will also use the append() method to insert the resultant value of adding k and the integers to a new list.

Example

The following program prints the list of integers after adding the k value to each list element using a "+" operator in Python.

# Initialising list, k value 
# and empty list
int_list = [5, 10, 15, 20]
k = 5
new_list = []

for x in int_list:
   new_list.append(x + k)

# Printing new list of integer
# with added k value
print(new_list)

Output

[10, 15, 20, 25]

Using List Comprehension and "+" Operator

List comprehension is a concise way of creating a list in Python, where we can perform an operation on each element of a given iterable object and store its resultant values as a list.

To add a k value to a list of integers in Python, we need to pass the list as an iterable and perform an addition (using the "+" operator) between a k value and the list items.

Example

The following program prints the list of integers after adding the k value to each list element using a list comprehension an "+" operator in Python.

# Initialising the list and k value 
orig_list = [5, 10, 15, 20]
k = 5

print ("The given list is : " + str(orig_list))

# Use list comprehension
new_list = [n + k for n in orig_list]

# printing result
print ("After adding 5 to each element to list : " + str(new_list))

Output

The given list is : [5, 10, 15, 20]
After adding 5 to each element to list : [10, 15, 20, 25]

Using map() Function and "+" Operator

The map() function in Python accepts two arguments, a function and an iterable. It returns a new iterable object by applying the specified function to each item of a given iterable.

To add a k value to a list of integers, we need to pass the list of integers as an iterable and a function to the map() function. This function adds the constant k value to integers in a list using the '+' operator.

Example

The following program prints the list of integers after adding the k value to each list element using the map() method and "+" operator in Python.

# Initialising list, k value 
orig_list = [5, 10, 15, 20]
k = 5

print ("The given list is : " + str(orig_list))

#Using map() + lambda
new_list= list(map(lambda m : m + k, orig_list))

# printing result
print (f"After adding {k} to each element to list : " + str(new_list))

Output

The given list is : [5, 10, 15, 20]
After adding 5 to each element to list : [10, 15, 20, 25]

Using a map() Function and add() Methods

In place of the "+" operator, we can also use the add() method of the Python operator module along with the map() function. The add() is used to add two numbers in Python.

We can create another list that has the same number of elements as the length of the list, and it contains the number that needs to be added. Then we pass both lists as an iterable to the map() function and perform addition between them using the operator module's add() method.

Note ? To use the add() method, we need to import its module.

Example

The following program prints the list of integers after adding the k value to each list element using the map() and add() methods in Python.

# Importing the operator module
import operator

# Initialising list, k value 
orig_list = [5, 10, 15, 20]
k = 5

print ("The given list is : " + str(orig_list))

# initializing new list
list_with_k_value = [k] * len(orig_list)

# using map() + operator.add
new_list = list(map(operator.add, orig_list, list_with_k_value))

# printing result
print(f"After adding {k} to each element to list :", new_list)

Output

The given list is : [5, 10, 15, 20]
After adding 5 to each element to list : [10, 15, 20, 25]

Using Numpy Library

We can also use the NumPy library's np.array method to add the k value to the list of integers in Python. The np.array() method converts the list into an array, enabling us to easily perform mathematical operations on it, such as adding a constant value k.

Note ? To use the np.aarray method, we need to import its module.

Example

The following program prints the list of integers after adding the k value to each list element using the Numpy library in Python.

# Importing Numpy's np method
import numpy as np

# Initialising list, k value 
orig_list = [5, 10, 15, 20]
k = 5

print ("The given list is : " + str(orig_list))

# Creating a new list
new_list = np.array(orig_list) + k

# printing result
print ("After adding 5 to each element to list : " + str(new_list))

Output

The given list is : [5, 10, 15, 20]
After adding 5 to each element to list : [10 15 20 25]

Conclusion

In this article, we have seen multiple approaches for adding a constant k value to list of integers in Python. The use of "+" operator is the simplest way for adding k value and list items but operator module's add() method and NumPy module's np.array method can also be effectively used for the same.

Updated on: 2020-02-18T11:31:07+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements