In the previous article, we have discussed Python Program for Double Factorial

Given a list and the task is to replace each element of a list by multiplying the previous and next elements.

The first element is replaced by multiplying the first and second elements.

The last element is replaced by multiplying the last and second last elements.

**Examples:**

**Example1:**

**Input:**

Given list = [7, 8, 1, 2, 3, 4]

**Output:**

The given List after replacing each element of a list by multiplying the previous and next elements: 56 7 16 3 8 12

**Explanation:**

Given List = [7*8, 7*1, 8*2, 1*3, 2*4, 3*4]

**Example2:**

**Input:**

Given list =[ 10 ,1 ,20, 2 ,30, 3]

**Output:**

The given List after replacing each element of a list by multiplying the previous and next elements: 10 200 2 600 6 90

**Explanation:**

Given List = [10*1, 10*20, 1*2, 20*30, 2*3, 30*3] = [10, 200, 2, 600, 6, 90]

## Program to Replace Every List Element by Multiplication of Previous and Next in Python

Below are the ways to replace each element of a list by multiplying the previous and next elements.

### Method #1: Using For Loop (Static Input)

**Approach:**

- Give the list as static input and store it in a variable.
- Calculate the length of the list using the len() function and store it in another variable.
- Check if the length of the given list is less than or equal to 1 using the if conditional statement.
- If the statement is true, do not print anything.
- Take a variable and initialize it with the first element of the given list say “previous”.
- Multiply the first and the next element of a list and store it in another variable.
- Loop from 1 to length of the given list -1 using the for loop.
- Inside the loop, assign the iterator value of the given list and store it in a

variable say “currnt”. - Multiply the above obtained “previous” with a given list of iterator+1 value

and store it in the given list of the iterator. - Assign the variable “currnt” to the “previus” and come out of the loop.
- Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
- print the given list after replacing each element of a list by multiplying the previous and next elements.
- The Exit of the Program.

**Below is the implementation:**

# Give the list as static input and store it in a variable. gvn_lst = [7, 8, 1, 2, 3, 4] # Calculate the length of the list using the len() function and # store it in another variable. lenth = len(gvn_lst) # Check if the length of the given list is less than or equal to 1 using the # if conditional statement. if lenth <= 1: # If the statement is true, do not print anything. print() # Take a variable and initialize it with the first element of the given list # say "previous". previus = gvn_lst[0] # Multiply the first and the next element of a list and store it in another variable. gvn_lst[0] = gvn_lst[0] * gvn_lst[1] # Loop from 1 to length of the given list -1 using the for loop. for itr in range(1, lenth-1): # Inside the loop, assign the iterator value of the given list and store it in a # variable say "currnt" currnt = gvn_lst[itr] # Multiply the above obtained "previous" with given list of iterator+1 value # and store it in given list of iterator. gvn_lst[itr] = previus * gvn_lst[itr+1] # Assign the variable "currnt" to the "previus" and come out of the loop. previus = currnt # Multiply the above obtained "previous" with given list of length-1 value # and store it in given list of length-1. gvn_lst[lenth-1] = previus * gvn_lst[lenth-1] # print the given list after replacing each element of a list by # multiplying the previous and next elements print("The given List after replacing each element of a list by multiplying the previous and next elements:") for itr in range(0, lenth): print(gvn_lst[itr], end=" ")

**Output:**

The given List after replacing each element of a list by multiplying the previous and next elements: 56 7 16 3 8 12

### Method #2: Using For loop (User Input)

**Approach:**

- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Calculate the length of the list using the len() function and store it in another variable.
- Check if the length of the given list is less than or equal to 1 using the if conditional statement.
- If the statement is true, do not print anything.
- Take a variable and initialize it with the first element of the given list say “previous”.
- Multiply the first and the next element of a list and store it in another variable.
- Loop from 1 to length of the given list -1 using the for loop.
- Inside the loop, assign the iterator value of the given list and store it in a

variable say “currnt”. - Multiply the above obtained “previous” with a given list of iterator+1 value

and store it in the given list of the iterator. - Assign the variable “currnt” to the “previus” and come out of the loop.
- Multiply the above obtained “previous” with a given list of length-1 values and store it in the given list of length-1.
- print the given list after replacing each element of a list by multiplying the previous and next elements.
- The Exit of the Program.

**Below is the implementation:**

# Give the list as user input using list(),map(),input(),and split() functions. #Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Calculate the length of the list using the len() function and # store it in another variable. lenth = len(gvn_lst) # Check if the length of the given list is less than or equal to 1 using the # if conditional statement. if lenth <= 1: # If the statement is true, do not print anything. print() # Take a variable and initialize it with the first element of the given list # say "previous". previus = gvn_lst[0] # Multiply the first and the next element of a list and store it in another variable. gvn_lst[0] = gvn_lst[0] * gvn_lst[1] # Loop from 1 to length of the given list -1 using the for loop. for itr in range(1, lenth-1): # Inside the loop, assign the iterator value of the given list and store it in a # variable say "currnt" currnt = gvn_lst[itr] # Multiply the above obtained "previous" with given list of iterator+1 value # and store it in given list of iterator. gvn_lst[itr] = previus * gvn_lst[itr+1] # Assign the variable "currnt" to the "previus" and come out of the loop. previus = currnt # Multiply the above obtained "previous" with given list of length-1 value # and store it in given list of length-1. gvn_lst[lenth-1] = previus * gvn_lst[lenth-1] # print the given list after replacing each element of a list by # multiplying the previous and next elements print("The given List after replacing each element of a list by multiplying the previous and next elements:") for itr in range(0, lenth): print(gvn_lst[itr], end=" ")

**Output:**

Enter some random List Elements separated by spaces = 10 1 20 2 30 3 The given List after replacing each element of a list by multiplying the previous and next elements: 10 200 2 600 6 90

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.