Multiplying elements in an array together (Python)

python multiply all elements in list together
python multiply function
python multiply list by float
how to multiply inputs in python
python list multiplication operator
python multiply array by scalar
python multiply an array by a number
python multiplying lists together

I'm attempting a question where I need to find the largest product of 2 digits contained within a long number:

Number = 73167176531330624919225119674426574742355349194934

lst = [int(i) for i in str(Number)]
print(lst)

new_lst = []

for i in lst:
    new_lst.append(i*(i+1))
    print(new_lst)

However, what I am attempting above gives the result 56, how do I multiply 7*3, then 3*1, and so on...

EDIT for H

Number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

lst = [int(i) for i in str(Number)]
print(lst)

new_lst = []

for i, value in enumerate(lst):
    new_lst.append(lst[i-13]*(lst[i-12])*(lst[i-11])*(lst[i-10])*(lst[i-9])*(lst[i-8])*(lst[i-7])*(lst[i-6])*(lst[i-5])*(lst[i-4])*(lst[i-3])*(lst[i-2])*(lst[i-1]))
print(max(new_lst))

You can iterate over a zip the list and a slice of it with the start differing by 1:

max(a * b for a, b in zip(lst, lst[1:]))

This returns: 54 (because of 9 and 6 in the number)

How to properly use the multiplication feature in python, How do you multiply every element in an array Python? numpy.multiply() in Python numpy.multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise. Syntax : numpy.multiply(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘multiply’)

You are currently multiplying the number i times i + 1. You could use range to do what you want like this:

for i in range(len(lst)-1):
        new_lst.append(lst[i]*(lst[i+1]))
        print(new_lst)

Multiplying elements in an array together (Python), You can iterate over a zip the list and a slice of it with the start differing by 1: max(​a * b for a, b in zip(lst, lst[1:])). This returns: 54 (because of 9  You can use multiply.reduce(a) (multiply is a function imported from numarray). With regular python you can also do: from operator import mul reduce(mul,a) This work even when 'a' is a plain python list. Actually, they'll both work with a plain python list. Multiply.reduce is much faster (see below) when operating on arrays, but somewhat slower

itertools has a recipe for this called pairwise.

>>> from itertools import tee
>>> def pairwise(iterable):
...     "s -> (s0,s1), (s1,s2), (s2, s3), ..."
...     a, b = tee(iterable)
...     next(b, None)
...     return zip(a, b)
... 
>>> [a*b for a,b in pairwise(map(int, '1234'))]
[2, 6, 12]

How do I multiply each element in a list by a number?, import numpy as np >>> >>> my_list = np.array([1, 2, 3, 4, 5]) >>> >>> my_list * 5 array([ 5, 10, 15, 20, 25]). Note that this doesn't work with Python's native lists. We are given an array and we have to calculate the product of an array using both iterative and recursive method. We initialize result as 1. We traverse array from left to right and multiply elements with result. # by "Sharad_Bhardwaj". // This code is contributed by nitin mittal. # by "Sharad_Bhardwaj". // This code is contributed by Nitin Mittal.

Number = 73167176531330624919225119674426574742355349194934

lst = [int(i) for i in str(Number)] print(lst)

new_lst = []

for i in xrange(len(lst)-1): new_lst.append(lst[i]*lst[i+1]) print new_lst

numpy - Multiply every element in an array, Python code example 'Multiply every element in an array' for the package numpy, powered by Kite. Know how to create arrays : array, arange, ones, zeros. Know the shape of the array with array.shape, then use slicing to obtain different views of the array: array[::2], etc. Adjust the shape of the array using reshape or flatten it with ravel. Obtain a subset of the elements of an array and/or modify their values with masks >>>

You want something like this perhaps:

Number = 73167176531330624919225119674426574742355349194934

print(max(int(x)*int(y) for x, y in zip(str(Number)[:-1], str(Number)[1:])))

Answer will be 54 (9*6)

How to multiply each element of a list by a number in Python, Use the syntax array * number with array as the previous result to multiply each element in array by number . a_list = [1, 2, 3]. I personally like this for a function that multiplies all elements of a generic list together: def multiply(n): total = 1 for i in range(0, len(n)): total *= n[i] print total It’s compact, uses simple things (a variable and a for loop), and feels intuitive to me (it looks like how I’d think of the problem, just take one, multiply it, then

Prod - Numpy and Scipy, a = np.array([1, 2, 3, 4]) Array multiplication is not matrix multiplication: add even elements with odd elements; Time them against their pure python counterparts ValueError: operands could not be broadcast together with shapes (4) (2). In this Python tutorial, we will learn how to perform multiplication of two matrices in Python using NumPy. Python is a programming language in addition that lets you work quickly and integrate systems more efficiently. However, In this tutorial, we will be solving multiplication of two matrices in the Python programming language.

1.4.2. Numerical operations on arrays, First we create 3 list for user input .here we use traversing technique. Initializing the value of product to 1, traverse all the element and multiply  Examples of how to perform mathematical operations on array elements ("element-wise operations") in python: Add a number to all the elements of an array Subtract a number to all the elements of an array

Python program to multiply all numbers in the list?, Deleting Items from a Python List. Because our array is populated with prickly and disagreeable hedgehogs, their spines are irritating one another  4.2 Arrays in Python In lower level languages common mathematical operations on arrays must be done ``manually''. For example, we might have a three element array that represents a vector.

Comments
  • You iterate over values, not indices. You may want to use for i in range(len(lst)) but even better would be for i, v in enumerate(lst). Have fun.
  • Must your "2 digits" be adjacent? Or are you looking for 81 as your end result (as there are multiple 9's).
  • @jpp they must be adjacent
  • IndexError: list index out of range
  • Feel free to edit and add checks @timgeb You could add a bunch of exception handling, but this is not the main issue here
  • you appear to forget the last number,list index out of range
  • @hakaishin - see edit, I've adapted your answer to fit in with the question i'm really trying to answer, I'm looking to make a loop with this
  • always better to be able to generalise a solution and make it into a function
  • Cool! Now I'm going to try and expand this to 13 digits, appreciate it
  • define str(Number) once before the max() call
  • assuming positive numbers only operator.mul(*map(int, max(zip(str(Number)[:-1], str(Number)[1:]))))
  • @Chris_Rands similar answer is already there using functional approach.