Jump to content

What are some ways to perform multiplication on a list of integers using loops in Python?


SubhanUllah

Recommended Posts

If you mean multiplying all the list items by some common factor, list comprehension is quick and clean. It has the same for loop implication

li = [1, 2, 3, 4, 5] 
 
print([2*i for i in li]) 
 
#OUTPUT 
[2, 4, 6, 8, 10] 
If you rather mean multipling all the items together to get a product, you can still use a for loop such as

li = [1, 2, 3, 4, 5] 
 
total = 1 
 
for i in li: 
        total = total * i 
 
print(total) 
 
#OUTPUT 
120 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...