Python Challenge - Product of Everything Else

This community-built FAQ covers the “Product of Everything Else” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Product of Everything Else

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This is my answer, not sure if there is a more efficient way:

def product_of_the_others(array):
new_list =
for i in range(len(array)):
temp_list = array[:i] + array[i+1:]
product = 1
for x in temp_list:
product = product * x
new_list.append(product)
return new_list

print(product_of_the_others([1, 2, 3, 4, 5]))

Space complexity, O(N); time complexity, O(N^2).

def product_of_the_others(array):
  arr = []
  for i, x in enumerate(array):
    temp = array[:]
    temp[i] = 1
    prod = 1
    for n in temp:
      prod *= n
    arr += [prod]
  return arr
  
print(product_of_the_others([1, 2, 3, 4, 5]))

This is how I solved it:

def product_of_the_others(array): # Write your code here position = 0 array_instance = [] new_list = [] for i in array: new_number = 1 array_instance = array.copy() array_instance.pop(position) for i in array_instance: new_number *= i position+=1 new_list.append(new_number) return new_list print(product_of_the_others([1, 2, 3, 4, 5]))
import numpy def remove_from_list(array, index): newList = array.copy() newList.pop(index) return newList def product_of_the_others(array): return [numpy.prod(remove_from_list(array, x)) for x in range(len(array))] print(product_of_the_others([1, 2, 3, 4, 5]))
from collections import deque def product_of_the_others(array): jar = deque(array) tulemus = 1 counter = 0 uus = [] while counter < len(jar): for i in range(1,len(jar)): tulemus *= jar[i] uus.append(tulemus) jar.rotate(-1) counter +=1 tulemus = 1 return uus print(product_of_the_others([1, 2, 3, 4, 5]))

The instructions state we are not permitted to use external libraries.

It’s also possible to use pop() and append() approaches, but I prefer deque.( jar.pop(0) and jar.append().

def product_of_the_others(array): tulemus = 1 counter = 0 uus = [] while counter < len(array): for i in range(1,len(array)): tulemus *= array[i] uus.append(tulemus) es = array.pop(0) array.append(es) counter +=1 tulemus = 1 return uus print(product_of_the_others([1, 2, 3, 4, 5]))

O(n)

def product_of_the_others(array):
  fullprod = 1; 
  zeroes = 0 
  for el in array: 
    if el != 0: 
      fullprod *= el
    else:
      zeroes += 1 
  return [0 if (zeroes and el != 0 or zeroes > 1 and el == 0) else fullprod/(el if el else 1) for el in array]
  
print(product_of_the_others([5,5,0,]))
def product_of_the_others(array): tulemus = 1 uus = [] for i in range(len(array)): for j in range (len(array)): if i != j: tulemus *=array[j] uus.append(tulemus) tulemus = 1 return uus print(product_of_the_others([1, 2, 3, 4, 5]))
1 Like
def product_of_the_others(array): product = 1 res = [] for i in range (len(array)): num_taken = array.pop(i) for j in range (len(array)): product *= array[j] res.append(product) array.insert(i, num_taken) product = 1 return res print(product_of_the_others([1, 2, 3, 4, 5]))

I wish this solution will help you all :smiley:

I first duplicates the original array with temp_array = array, but then all chages on the temp_array will effect the array itself as well.

def product_of_the_others(array): products = [] for i in range(len(array)): result = 1 temp_array = [] for x in array: temp_array.append(x) temp_array.remove(array[i]) for y in temp_array: result *= y products.append(result) return products print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array): # Write your code here storage=1 newArray=[] for x in range(len(array)): for y in range(len(array)): if x==y: pass else: storage=storage*array[y] newArray.append(storage) storage=1 return(newArray) print(product_of_the_others([1, 2, 3, 4, 5]))