{"id":18202,"date":"2021-08-26T10:42:14","date_gmt":"2021-08-26T05:12:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=18202"},"modified":"2021-11-22T18:37:14","modified_gmt":"2021-11-22T13:07:14","slug":"python-program-to-replace-every-list-element-by-multiplication-of-previous-and-next","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-replace-every-list-element-by-multiplication-of-previous-and-next\/","title":{"rendered":"Python Program to Replace Every List Element by Multiplication of Previous and Next"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Double Factorial<\/a> The first element is replaced by multiplying the first and second elements.<\/p>\n The last element is replaced by multiplying the last and second last elements.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Below are the ways to replace each element of a list by multiplying the previous and next elements.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nGiven a list and the task is to replace each element of a list by multiplying the previous and next elements.<\/p>\nGiven list = [7, 8, 1, 2, 3, 4]<\/pre>\n
The given List after replacing each element of a list by multiplying the previous and next elements:\r\n56 7 16 3 8 12<\/pre>\n
Given List = [7*8, 7*1, 8*2, 1*3, 2*4, 3*4]<\/pre>\n
Given list =[ 10 ,1 ,20, 2 ,30, 3]<\/pre>\n
The given List after replacing each element of a list by multiplying the previous and next elements:\r\n10 200 2 600 6 90<\/pre>\n
Given List = [10*1, 10*20, 1*2, 20*30, 2*3, 30*3] = [10, 200, 2, 600, 6, 90]<\/pre>\n
Program to Replace Every List Element by Multiplication of Previous and Next in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
\nvariable say “currnt”.<\/li>\n
\nand store it in the given list of the iterator.<\/li>\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [7, 8, 1, 2, 3, 4]\r\n# Calculate the length of the list using the len() function and\r\n# store it in another variable.\r\nlenth = len(gvn_lst)\r\n# Check if the length of the given list is less than or equal to 1 using the\r\n# if conditional statement.\r\nif lenth <= 1:\r\n # If the statement is true, do not print anything.\r\n print()\r\n # Take a variable and initialize it with the first element of the given list\r\n# say \"previous\".\r\nprevius = gvn_lst[0]\r\n# Multiply the first and the next element of a list and store it in another variable.\r\ngvn_lst[0] = gvn_lst[0] * gvn_lst[1]\r\n# Loop from 1 to length of the given list -1 using the for loop.\r\nfor itr in range(1, lenth-1):\r\n # Inside the loop, assign the iterator value of the given list and store it in a\r\n # variable say \"currnt\"\r\n currnt = gvn_lst[itr]\r\n# Multiply the above obtained \"previous\" with given list of iterator+1 value\r\n# and store it in given list of iterator.\r\n gvn_lst[itr] = previus * gvn_lst[itr+1]\r\n # Assign the variable \"currnt\" to the \"previus\" and come out of the loop.\r\n previus = currnt\r\n# Multiply the above obtained \"previous\" with given list of length-1 value\r\n# and store it in given list of length-1.\r\ngvn_lst[lenth-1] = previus * gvn_lst[lenth-1]\r\n# print the given list after replacing each element of a list by\r\n# multiplying the previous and next elements\r\nprint(\"The given List after replacing each element of a list by multiplying the previous and next elements:\")\r\nfor itr in range(0, lenth):\r\n print(gvn_lst[itr], end=\" \")\r\n<\/pre>\n
The given List after replacing each element of a list by multiplying the previous and next elements:\r\n56 7 16 3 8 12<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
\nvariable say “currnt”.<\/li>\n
\nand store it in the given list of the iterator.<\/li>\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n#Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the list using the len() function and\r\n# store it in another variable.\r\nlenth = len(gvn_lst)\r\n# Check if the length of the given list is less than or equal to 1 using the\r\n# if conditional statement.\r\nif lenth <= 1:\r\n # If the statement is true, do not print anything.\r\n print()\r\n # Take a variable and initialize it with the first element of the given list\r\n# say \"previous\".\r\nprevius = gvn_lst[0]\r\n# Multiply the first and the next element of a list and store it in another variable.\r\ngvn_lst[0] = gvn_lst[0] * gvn_lst[1]\r\n# Loop from 1 to length of the given list -1 using the for loop.\r\nfor itr in range(1, lenth-1):\r\n # Inside the loop, assign the iterator value of the given list and store it in a\r\n # variable say \"currnt\"\r\n currnt = gvn_lst[itr]\r\n# Multiply the above obtained \"previous\" with given list of iterator+1 value\r\n# and store it in given list of iterator.\r\n gvn_lst[itr] = previus * gvn_lst[itr+1]\r\n # Assign the variable \"currnt\" to the \"previus\" and come out of the loop.\r\n previus = currnt\r\n# Multiply the above obtained \"previous\" with given list of length-1 value\r\n# and store it in given list of length-1.\r\ngvn_lst[lenth-1] = previus * gvn_lst[lenth-1]\r\n# print the given list after replacing each element of a list by\r\n# multiplying the previous and next elements\r\nprint(\"The given List after replacing each element of a list by multiplying the previous and next elements:\")\r\nfor itr in range(0, lenth):\r\n print(gvn_lst[itr], end=\" \")\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 1 20 2 30 3\r\nThe given List after replacing each element of a list by multiplying the previous and next elements:\r\n10 200 2 600 6 90<\/pre>\n
\n