Python Forum
How do i multiply elements in a list together
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i multiply elements in a list together
#1
How can I multiply all elements in a list together with a for loop?
Reply
#2
What have you tried?
Reply
#3
Edit: zip is not required for this task, because you've only one list.

If you have two Iterables (sequences), you can use the builtin function zip together with a for-loop or a list-comprehension. This are the building-blocks. In the for-loop you multiply one element from the first list, with one element on the second list. Then you append each result to a new list. In the case if you use a list-comprehension, the empty list is not needed and the code shrinks, because you don't need define an empty list and you don't need to append. For simple tasks, the list-comprehension is very handy. If you've a complex situation (catching Exceptions, branching, ...) a for-loop may be better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
@DeaD_EyE, OP wants to multiply all elements in single list (reduce) not two iterables
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Edit: Sorry, in this case it's called Broadcasting and you don't need the zip, because the value you use to multiply, does not change. But it's still important to know the zip-function for future tasks.

So this task don't require the zip and a list-comprehension is the ideal candidate for this.
Btw: The solution is in the Python-Documentation: https://docs.python.org/3/tutorial/datas...rehensions
It took me one year to understand the structure of this document :-D
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
Although list comprehension is great tool, it's not what OP wants :-)
Sorry, Dead_EyE, I don't mean to snap, but obviously OP is confused enough and you are not helping by suggesting list comprehension.

(Mar-08-2020, 11:13 AM)Olavv Wrote: How can I multiply all elements in a list together with a for loop?
from their question, if they have [1, 2, 3] the result they look for is 1*2*3, i.e. 6

And because this looks very much like homework we don't give the solution without effort from OP
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Mar-08-2020, 12:17 PM)buran Wrote: Although list comprehension is great tool, it's not what OP wants :-)
Sorry, Dead_EyE, I don't mean to snap, but obviously OP is confused enough and you are not helping by suggesting list comprehension.

(Mar-08-2020, 11:13 AM)Olavv Wrote: How can I multiply all elements in a list together with a for loop?
from their question, if they have [1, 2, 3] the result they look for is 1*2*3, i.e. 6

And because this looks very much like homework we don't give the solution without effort from OP
It's okay. I figured it out
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 430 Jan-27-2024, 04:03 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 475 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Checking if a string contains all or any elements of a list k1llcod3 1 1,095 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to change the datatype of list elements? mHosseinDS86 9 1,968 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,116 May-17-2022, 11:38 AM
Last Post: Larz60+
  Why am I getting list elements < 0 ? Mark17 8 3,128 Aug-26-2021, 09:31 AM
Last Post: naughtyCat
  Looping through nested elements and updating the original list Alex_James 3 2,122 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  Extracting Elements From A Website List knight2000 2 2,258 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Make Groups with the List Elements quest 2 1,969 Jul-11-2021, 09:58 AM
Last Post: perfringo
  I cannot delete and the elements from the list quest 4 2,974 May-11-2021, 12:01 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020