Saturday, November 16, 2024
Google search engine
HomeLanguagesPython program to replace first ‘K’ elements by ‘N’

Python program to replace first ‘K’ elements by ‘N’

Given a List, replace first K elements by N.

Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 4, N = 3 
Output : [3, 3, 3, 3, 4, 2, 6, 9] 
Explanation : First 4 elements are replaced by 3.

Input : test_list = [3, 4, 6, 8, 4, 2, 6, 9], K = 2, N = 10 
Output : [10, 10, 6, 8, 4, 2, 6, 9] 
Explanation : First 2 elements are replaced by 10. 
 

Method 1: Using a loop

This naive method iterates through the list ‘k‘ number of times and assigns ‘N‘ to each element encountered till the loop ends.

Python3




# initializing list
test_list = [3, 4, 6, 8, 4, 2, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing N
N = 6
 
# assigning value 'N' till K elements
for idx in range(K):
 
    test_list[idx] = N
 
# printing result
print("Elements after replacement : " + str(test_list))


Output

The original list is : [3, 4, 6, 8, 4, 2, 6, 9]
Elements after replacement : [6, 6, 6, 6, 6, 2, 6, 9]

 
Method 2: Using list slicing

 In this, we slice off, just a few elements from the list and assign ‘N‘ to the sliced list.

Python3




# initializing list
test_list = [3, 4, 6, 8, 4, 2, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing N
N = 6
 
# assigning the N value till K elements
test_list[: K] = [N] * K
 
# printing result
print("Elements after replacement : " + str(test_list))


Output

The original list is : [3, 4, 6, 8, 4, 2, 6, 9]
Elements after replacement : [6, 6, 6, 6, 6, 2, 6, 9]

Method 3: Using list(),map(),join() and replace()

Initially convert each element of list to string and then join the list using empty string.Replace first K characters with N in that joined list(which is a string) ,later convert each element of a string to integer and make it a list.

Python3




# initializing list
test_list = [3, 4, 6, 8, 4, 2, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing N
N = 6
 
# assigning the N value till K elements
a=list(map(str,test_list))
b="".join(a)
b=b.replace(b[:K],str(N)*K)
b=list(map(int,b))
# printing result
print("Elements after replacement : " + str(b))


Output

The original list is : [3, 4, 6, 8, 4, 2, 6, 9]
Elements after replacement : [6, 6, 6, 6, 6, 2, 6, 9]

Method 4:Using list comprehension

Algorithm

  1. Initialize the list test_list with some values.
  2. Initialize the value of K and N.
  3. Iterate over the first K elements of the list using a for loop and list comprehension.
  4. If the index is less than K, replace the value at that index with N, else keep the original value.
  5. Print the updated list test_list.

Python3




# initializing list
test_list = [3, 4, 6, 8, 4, 2, 6, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# initializing N
N = 6
 
# replacing elements using list comprehension
test_list = [N if idx < K else val for idx, val in enumerate(test_list)]
 
# printing result
print("Elements after replacement : " + str(test_list))
#This code is contributed by Vinay Pinjala.


Output

The original list is : [3, 4, 6, 8, 4, 2, 6, 9]
Elements after replacement : [6, 6, 6, 6, 6, 2, 6, 9]

Time complexity:

Initializing the list takes O(n) time, where n is the length of the list.
Initializing K and N takes constant time, i.e., O(1).
The for loop that iterates over the first K elements takes O(K) time.
The list comprehension takes O(K) time.
Printing the updated list takes O(n) time.
Therefore, the overall time complexity is O(n + K).
Space complexity:

The space required to store the input list test_list is O(n).
The space required to store the variables K and N is constant, i.e., O(1).
The space required by the list comprehension is also O(n).
Therefore, the overall space complexity is O(n).

Method 5 :  use the extend() method of lists

Steps:

Initialize the list test_list.
Initialize the values of K and N.
Use list slicing to select the sublist of test_list up to index K.
Replace the selected sublist with a list of K copies of N using the = assignment operator and the * operator.
Print the resulting list.

Python3




test_list = [3, 4, 6, 8, 4, 2, 6, 9]
K = 5
N = 6
 
# replace the first K elements with N using extend()
test_list[:K] = [N] * K
 
print("Elements after replacement : " + str(test_list))


Output

Elements after replacement : [6, 6, 6, 6, 6, 2, 6, 9]

Time complexity:  O(K) 

Auxiliary space:  O(K) 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments