Python – Multiply K to every Nth element
We generally wish to employ a particular function to all the elements in a list. But sometimes, according to requirement we would wish to employ a particular functionality to certain elements of the list, basically to every Nth element in list. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + enumerate() The functionality of getting every Nth number of list can be done with the help of list comprehension and enumerate function helps in the iteration of the whole list.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ("The original list is : " + str (test_list))
N = 3
K = 2
res = [i * K if j % N = = 0 else i for j, i in enumerate (test_list)]
print ("The list after multiplying K to every Nth element : " + str (res))
|
Output :
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after multiplying K to every Nth element : [2, 4, 5, 12, 7, 8, 18, 12]
Time complexity: O(n) where n is the length of the input list.
Auxiliary space: O(n) as we are creating a new list of the same length as the input list.
Method #2 : Using list comprehension + list slicing Above mentioned functions can help to perform these tasks. The list comprehension does the task of iteration in list and list slicing does the extraction of every Nth element.
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ("The original list is : " + str (test_list))
N = 3
K = 2
test_list[ 0 :: 3 ] = [i * K for i in test_list[ 0 :: N]]
print ("The list after multiplying K to every Nth element : "
+ str (test_list))
|
Output :
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after multiplying K to every Nth element : [2, 4, 5, 12, 7, 8, 18, 12]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space complexity: O(1),
Method #3: Using For Loop
Python3
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
N = 3
K = 2
for i in range ( 0 , len (test_list), N):
test_list[i] = test_list[i] * K
print ( "The list after multiplying K to every Nth element : " + str (test_list))
|
Output
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after multiplying K to every Nth element : [2, 4, 5, 12, 7, 8, 18, 12]
Time complexity: O(n)
Auxiliary space: O(1)
Method#4: Using Recursive method.
Python3
def multiply_nth_element(test_list, N, K, index = 0 ):
if index > = len (test_list):
return test_list
test_list[index] * = K
return multiply_nth_element(test_list, N, K, index + N)
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
N = 3
K = 2
result = multiply_nth_element(test_list, N, K)
print ( "The list after multiplying K to every Nth element : " + str (result))
|
Output
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after multiplying K to every Nth element : [2, 4, 5, 12, 7, 8, 18, 12]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using numpy array
- Import numpy library and initialize the original list, N and K values.
- Convert the original list to numpy array.
- Use numpy’s indexing feature to select the Nth elements and multiply them by K.
- Convert the modified numpy array back to a list.
- Print the original list after updating the Nth element.
Python3
import numpy as np
test_list = [ 1 , 4 , 5 , 6 , 7 , 8 , 9 , 12 ]
print ( "The original list is : " + str (test_list))
N = 3
K = 2
np_array = np.array(test_list)
np_array[::N] = np_array[::N] * K
test_list = np_array.tolist()
print ( "The list after multiplying K to every Nth element : " + str (test_list))
|
Output:
The original list is : [1, 4, 5, 6, 7, 8, 9, 12]
The list after multiplying K to every Nth element : [2, 4, 5, 12, 7, 8, 18, 12]
Time complexity: O(n)
Auxiliary space: O(n)
Last Updated :
16 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...