Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Specific Range Addition in List

Improve Article
Save Article
  • Last Updated : 15 Mar, 2023
Improve Article
Save Article

Sometimes, while working with Python, we need to perform edition in Python list. And sometimes we need to perform this to specific index range in it. This kind of application can have application in many domains. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop This is brute way in which this task can be performed. In this, we just iterate through the specified range in which the edition has to be performed. 

Python3




# Python3 code to demonstrate
# Specific Range Addition in List
# using loop
 
# Initializing list
test_list = [4, 5, 6, 8, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 5
 
# Specific Range Addition in List
# using loop
for idx in range(i, j):
    test_list[idx] += 3
 
# printing result
print ("List after range addition : " + str(test_list))

Output : 

The original list is : [4, 5, 6, 8, 10, 11]
List after range addition : [4, 5, 9, 11, 13, 11]

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(1), constant extra space is needed

  Method #2 : Using list comprehension This task can also be performed using list comprehension. This method uses same way as above but its a shorthand to above. 

Python3




# Python3 code to demonstrate
# Specific Range Addition in List
# using list comprehension
 
# Initializing list
test_list = [4, 5, 6, 8, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 5
 
# Specific Range Addition in List
# using list comprehension
test_list[i : j] = [ele + 3 for ele in test_list[i : j]]
 
# printing result
print ("List after range addition : " + str(test_list))

Output : 

The original list is : [4, 5, 6, 8, 10, 11]
List after range addition : [4, 5, 9, 11, 13, 11]

Method #3 : Using numpy Here is one approach using numpy library:

Note: Install numpy module using command “pip install numpy”

Python3




# Importing numpy library
import numpy as np
 
# Initializing list
test_list = [4, 5, 6, 8, 10, 11]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing range
i, j = 2, 5
 
# Specific Range Addition in List using numpy
test_list = np.array(test_list)
test_list[i:j] += 3
 
# printing result
print ("List after range addition : " + str(test_list.tolist()))
#This code is contributed by Edula Vinay Kumar Reddy

Output:

The original list is : [4, 5, 6, 8, 10, 11]
List after range addition : [4, 5, 9, 11, 13, 11]

Time Complexity: O(n), where n is the length of the list.
Space Complexity: O(n), as a numpy array of the same length as the list is created.

Explanation:

Import the numpy library
Convert the list to a numpy array
Add 3 to the specific range (i:j) of the numpy array
Convert the numpy array back to a list and print the result.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!