Python - Specific Range Addition in List
Last Updated :
25 Apr, 2025
Sometimes, while working with Python, we need to perform an edition in the Python list. And sometimes we need to perform this to a specific index range in it. This kind of application can have applications in many domains. Let us discuss certain ways in which this task can be performed.
Method #1: Using loop
This is a 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 the same way as above but it's a shorthand for the 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.
Auxiliary Space: 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.
Method 4: Using the slice notation and the built-in map function.
Python3
# Initializing list
test_list = [4, 5, 6, 8, 10, 11]
# Initializing range(custom)
i, j = 2, 5
# Specific Range Addition in List using map and slice
test_list[i:j] = map(lambda x: x + 3, test_list[i:j])
# printing result
print("List after range addition : " + str(test_list))
OutputList after range addition : [4, 5, 9, 11, 13, 11]
Time complexity of O(n), where n is the length of the list.
Auxiliary space: O(1), as we're only modifying the original list and not creating any additional data structures.
Similar Reads
Python | Alternate range slicing in list
List slicing is quite common utility in Python, one can easily slice certain elements from a list, but sometimes, we need to perform that task in non-contiguous manner and slice alternate ranges. Let's discuss how this particular problem can be solved. Method #1 : Using list comprehension List compr
6 min read
Python | Decimal step range in list
Sometimes, while working with a Python list we can have a problem in which we require to populate the list in a range of decimals. Integral ranges are easier to handle using inbuilt constructs, but having a decimal value to provide to range value doesn't work. Let's discuss a way in which this probl
4 min read
Slicing range() function in Python
range() allows users to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next.range() takes
2 min read
Addition in Nested Tuples - Python
Sometimes, while working with records, we can have a problem in which we require to perform index wise addition of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let's discuss certain ways in which this problem can be solved. Method #1: U
6 min read
Python - Pairwise Addition in Tuples
Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element addition. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + ge
4 min read
Python | Rear Addition of Record
Sometimes, while working with Python list, we can have a problem in which we need to add a new tuple to existing list. Append at rear is usually easier than addition at front. Letâs discuss certain ways in which this task can be performed. Method #1 : Using insert() This is one of the way in which t
6 min read
Python - Concatenate Ranged Values in String list
Given list of strings, perform concatenation of ranged values from the Strings list. Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 Output : FGorll Explanation : All string sliced, FG, or and ll from all three strings and concatenated. Input : test_list = ["aGFGcs", "cforef", "aa
5 min read
Split a Python List into Sub-Lists Based on Index Ranges
The task of splitting a Python list into sub-lists based on index ranges involves iterating through a list of index pairs and extracting the corresponding slices. Given a list a and a list of tuples b, where each tuple represents a start and end index, the goal is to create sub-lists that include el
3 min read
Index Specific Cyclic Iteration in List - Python
When working with lists in Python, there may be cases where we need to iterate over a list starting from a specific index in a cyclic manner. For example, given the list [10, 20, 30, 40, 50], we may want to start iterating from index 2 (i.e., 30) and continue iterating in a cyclic manner. There are
3 min read
Python program to reverse a range in list
Reversing a specific range within a list is a common task in Python programming that can be quite useful, when data needs to be rearranged. In this article, we will explore various approaches to reverse a given range within a list. We'll cover both simple and optimized methods. One of the simplest w
3 min read