- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Consecutive element maximum product in Python

Python has great libraries to manipulate data. We may come across a need to find the maximum product of two consecutive numbers which are part of the big string. In this article we will see the ways to achieve that.

## With zip and max

We convert the string into a list. Then create pairs from the consecutive elements with help of slicing. Applying * we multiply the pair and then take the max value from the result of the multiplication from each of the pairs.

## Example

Astring = '5238521' # Given string print("Given String : ",Astring) # Convert to list Astring = list(Astring) print("String converted to list:\n",Astring) # Using max() res = max(int(a) * int(b) for a, b in zip(Astring, Astring[1:])) # Result print("The maximum consecutive product is : " ,res)

## Output

Running the above code gives us the following result −

Given String : 5238521 String converted to list: ['5', '2', '3', '8', '5', '2', '1'] The maximum consecutive product is : 40

## With map and max

We take a similar approach as above. But we use map function to keep generating the pair of consecutive integers. Then use the mul function from operator module to multiply the numbers in this pair. Finally apply the max function to get the max value of the result.

## Example

from operator import mul Astring = '5238521' # Given string print("Given String : ",Astring) # Convert to list Astring = list(Astring) print("String converted to list:\n",Astring) # Using max() res = max(map(mul, map(int, Astring), map(int, Astring[1:]))) # Result print("The maximum consecutive product is : " ,res)

## Output

Running the above code gives us the following result −

Given String : 5238521 String converted to list: ['5', '2', '3', '8', '5', '2', '1'] The maximum consecutive product is : 40

- Related Articles
- Maximum Product Subarray in Python
- Maximum sum and product of the M consecutive digits in a number in C++
- Maximum element in tuple list in Python
- Program to find maximum subarray min-product in Python
- Python – Sort by Maximum digit in Element
- Program to find maximum product of contiguous subarray in Python
- Python – Maximum of K element in other list
- Find missing element in a sorted array of consecutive numbers in Python
- Program to find maximum number of consecutive values you can make in Python
- Maximum Product Subarray | Added negative product case in C++
- Maximum consecutive repeating character in string in C++
- Python - Sort Matrix by Maximum Row element
- Program to find maximum non negative product in a matrix in Python
- Maximum consecutive 1s after n swaps in JavaScript
- Program to find maximum length of subarray with positive product in Python