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 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)) |
The original list is : [4, 5, 6, 8, 10, 11] List after range addition : [4, 5, 9, 11, 13, 11]
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 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)) |
The original list is : [4, 5, 6, 8, 10, 11] List after range addition : [4, 5, 9, 11, 13, 11]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Index specific cyclic iteration in list
- Python | Sort list of tuples by specific ordering
- Python | Find the list elements starting with specific letter
- Python | Remove element from given list containing specific digits
- Python - Specific case change in String List
- Python - Binary operation on specific keys in Dictionary List
- Python - Specific Characters Frequency in String List
- Python program to extract only the numbers from a list which have some specific digits
- Addition and Blending of images using OpenCV in Python
- Python | Addition of tuples
- Addition in Nested Tuples - Python
- Python - Pairwise Addition in Tuples
- Python | Rear Addition of Record
- Python program for addition and subtraction of complex numbers
- Python - Diagonal element addition among lists
- Python | Alternate character addition
- Python - Row-wise element Addition in Tuple Matrix
- Addition and Subtraction on TimeDelta objects using Pandas - Python
- Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
- Perform addition and subtraction using CherryPy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

