Program to Add Number to each Element in a List

Python Program to Add Number to each Element in a List

In the previous article, we have discussed Python Program to Check a Binary Number is Divisible by a number N.
Given a list and the task is to add a given input number to each element in a list.

Examples:

Example1:

Input:

Given list = [2, 4, 6, 8, 10]
Given number = 5

Output:

The given list after addition of a number to each element in a list =  [7, 9, 11, 13, 15]

Example2:

Input:

Given list = [1, 4, 6, 2, 9]
Given number = 10

Output:

The given list after addition of a number to each element in a list = [11, 14, 16, 12, 19]

Program to Add Number to each Element in a List

Below are the ways to add a given input number to each element in a list.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the List as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop in the above-given list using the for loop.
  • Inside the loop, Increment the iterator value of the list by the given number and store it in the same variable
  • Print the given list after the addition of a given number to each element in a list.
  • The Exit of the program.

Below is the implementation:

# Give the List as static input and store it in a variable.
gven_lst = [2,4,6,8,10]
# Give the number as static input and store it in another variable.
numbr = 5
# Calculate the length of the given list using len() function and store it
# in another variable.
len_lst = len(gven_lst)
# Loop in the above-given list using the for loop.
for i in range(len_lst):
  # Inside the loop, Increment the iterator value of the list by the given number
  # and store it in the same variable
    gven_lst[i] = gven_lst[i]+numbr
# Print the given list after the addition of a given number to each element in a list .
print("The given list after addition of a number to each element in a list = ", gven_lst)

Output:

The given list after addition of a number to each element in a list =  [7, 9, 11, 13, 15]

Method #2: Using For Loop (User Input)

Approach:

  • Give the List as user input using list(),map(),input(),and split() functions and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Calculate the length of the given list using the len() function and store it in another variable.
  • Loop in the above-given list using the for loop.
  • Inside the loop, Increment the iterator value of the list by the given number and store it in the same variable
  • Print the given list after the addition of a given number to each element in a list.
  • The Exit of the program.

Below is the implementation:

# Give the List as user input using list(),map(),input(),and split() functions and 
#store it in a variable.
gven_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the number as user input using the int(input()) function and 
#store it in another variable.
numbr = int(input('Enter some random number = '))
# Calculate the length of the given list using len() function and store it
# in another variable.
len_lst = len(gven_lst)
# Loop in the above-given list using the for loop.
for i in range(len_lst):
  # Inside the loop, Increment the iterator value of the list by the given number
  # and store it in the same variable
    gven_lst[i] = gven_lst[i]+numbr
# Print the given list after the addition of a given number to each element in a list .
print("The given list after addition of a number to each element in a list = ", gven_lst)

Output:

Enter some random List Elements separated by spaces = 1 4 6 2 9
Enter some random number = 10
The given list after addition of a number to each element in a list = [11, 14, 16, 12, 19]

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.