Python - Specific Range Addition in List
Last Updated :
28 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
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
Python | Assign range of elements to List Assigning elements to list is a common problem and many varieties of it have been discussed in the previous articles. This particular article discusses the insertion of range of elements in the list. Let's discuss certain ways in which this problem can be solved. Method #1 : Using extend() This can
5 min read
Creating a List of Sets in Python Creating a list of sets in Python involves storing multiple sets within list structure. Each set can contain unique elements and the list can hold as many sets as needed. In this article, we will see how to create a list of set using different methods in Python.Using List ComprehensionList comprehen
2 min read
Python - Extract Elements from Ranges in List We are given a list and list containing tuples we need to extract element from ranges in tuples list. For example, n = [10, 20, 30, 40, 50, 60, 70, 80, 90] and r = [(1, 3), (5, 7)] (ranges) we need to extract elements so that output should be [[20, 30, 40], [60, 70, 80]].Using List ComprehensionList
3 min read
Python - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Python - Adding Tuple to List and Vice - Versa Adding a tuple to a list in Python can involve appending the entire tuple as a single element or merging its elements into the list. Conversely, adding a list to a tuple requires converting the list to a tuple and concatenating it with the existing tuple, as tuples are immutable. For example, adding
4 min read