
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Adding K to each element in a Python list of integers
In data analysis, sometimes it becomes necessary to add some value to each element in a python list to judge about the outcome of a new scenario. This helps in testing the multiple scenarios on how the data set will behave with different values hence make a model or algorithms which can handle those scenarios. In this article, we will see how we can handle this requirement.
Using List Comprehension
List comprehension is a normal way of handling the list where we loop through each element of the list. In the below example we add the same number to each element of the list using a simple for loop.
Example
orig_list = [5, 6, 7, 4, 10] print ("The given list is : " + str(orig_list)) # Use list comprehension new_list = [n + 5 for n in orig_list] # printing result print ("After adding 5 to each element to list : " + str(new_list))
Output
Running the above code gives us the following result −
The given list is : [5, 6, 7, 4, 10] After adding 5 to each element to list : [10, 11, 12, 9, 15]
Using lambda with map
The map and add method can also give us the same result. Lambda functions repeats the same action for affixed number of iterations and map is used to capture the result after all the lambda iterations are over.
Example
orig_list = [5, 6, 7, 4, 10] print ("The given list is : " + str(orig_list)) #Using map() + lambda new_list= list(map(lambda m : m + 3, orig_list)) print ("After adding i to each element to list : " + str(new_list))
Output
Running the above code gives us the following result −
The given list is : [5, 6, 7, 4, 10] After adding i to each element to list : [8, 9, 10, 7, 13]
Using map() and add()
In place of the lambda operator, we can also use the add method along with map. In the below example, we create another list which has same number of elements as the length of the list and it contains the number which needs to be added. Then we apply the map method.
Example
import operator orig_list = [5, 6, 7, 4, 10] print ("The given list is : " + str(orig_list)) # initializing new list list_with_k_value = [9] * len(orig_list) # using map() + operator.add new_list = list(map(operator.add, orig_list, list_with_k_value)) print ("After adding i2 to each element to list : " + str(new_list))
Output
Running the above code gives us the following result −
The given list is : [5, 6, 7, 4, 10] After adding i2 to each element to list : [14, 15, 16, 13, 19]
- Related Articles
- Python – Maximum of K element in other list
- Adding an element to the List in C#
- Python – Extract element from a list succeeded by K
- Python program to find the group sum till each K in a list
- Update each element in tuple list in Python
- Convert number to list of integers in Python
- Convert list of numerical string to list of Integers in Python
- Python Program to find the cube of each list element
- Python program to get the indices of each element of one list in another list
- Python – Find the frequency of numbers greater than each element in a list
- Python program to print duplicates from a list of integers?
- Program to find a list of numbers where each K-sized window has unique elements in Python
- Program to find sum of concatenated pairs of all each element in a list in Python?\n
- Write a program in C++ to find the top K frequent element in an array of integers
- Python - Given a list of integers that represents a decimal value, increment the last element by 1
