Python | Adding K to each element in a list of integers
Last Updated :
18 Apr, 2023
While working with the Python lists, we can come over a situation in which we require to add the integer k to each element in the list. We possibly need to iterate and add k to each element but that would increase the line of code. Let’s discuss certain shorthands to perform this task.
Method #1: Using List Comprehension List comprehension is just the short way to perform the task we perform using the naive method. This is mainly useful to save time and also is best among others when it comes to the readability of the code.
Python3
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 4
res = [x + K for x in test_list]
print ( "The list after adding K to each element : " + str (res))
|
Output :
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2: Using map() + lambda map function can be used to pair each element with the lambda function which performs the task of adding K to each element in the list.
Python3
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 4
res = list ( map ( lambda x: x + K, test_list))
print ( "The list after adding K to each element : " + str (res))
|
Output :
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
Method #3 : Using map() + operator.add This is similar to the above function but uses the operator.add to add each element to other element from the other list of K formed before applying the map function. It adds similar index elements of list.
Python3
import operator
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K_list = [ 4 ] * len (test_list)
res = list ( map (operator.add, test_list, K_list))
print ( "The list after adding K to each element : " + str (res))
|
Output :
The original list is : [4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
Method #4: Using zip function
To add two lists, you can use the zip function in combination with a list comprehension.
Here is an example:
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 4 ] * len (list1)
result = [x + y for x, y in zip (list1, list2)]
print (result)
|
Time complexity: O(N)
Auxiliary Space: O(N)
Method #5:Using the built-in list method extend()’
Step-by-step algorithm:
- Initialize an empty list “res”.
- Iterate through each element x in the “test_list”.
- Add K to the current element x and append the result to the “res” list using extend method.
- Print the final “res” list.
Python3
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is :" + str (test_list))
K = 4
res = []
res.extend(x + K for x in test_list)
print ( "The list after adding K to each element : " + str (res))
|
Output
The original list is :[4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
Time Complexity: O(n), where n is the length of “test_list”. This is because we need to iterate through each element of the “test_list” once.
Auxiliary Space: O(n), where n is the length of “test_list”.
Method #6: Using numpy library
- Importing the numpy library
- initializing a list
- Converting the list into an array using numpy.array()
- Adding K to each element using numpy library
- Converting back to list
- Printing result
Python3
import numpy as np
test_list = [ 4 , 5 , 6 , 3 , 9 ]
print ( "The original list is : " + str (test_list))
K = 4
arr = np.array(test_list)
res = arr + K
res = res.tolist()
print ( "The list after adding K to each element : " + str (res))
|
Output
The original list is :[4, 5, 6, 3, 9]
The list after adding K to each element : [8, 9, 10, 7, 13]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...