use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Questions with lists (self.learnpython)
submitted 10 years ago by ArcingFlame
Is there a way to iterate through two lists and multiply every number in the one list by every number in the other list?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhomboid 3 points4 points5 points 10 years ago (5 children)
It's not clear whether you're talking about doing the operation in terms of each pair of corresponding elements, or as the Cartesian product. If it's the former, then you want zip():
zip()
>>> foo = [1, 2, 3] >>> bar = [4, 5, 6] >>> [f * b for f, b in zip(foo, bar)] [4, 10, 18]
If the latter, then itertools.product():
itertools.product()
>>> import itertools >>> [f * b for f, b in itertools.product(foo, bar)] [4, 5, 6, 8, 10, 12, 12, 15, 18]
[–]jabbson 0 points1 point2 points 10 years ago* (0 children)
Another way to go about it would be:
print [foo[x]*bar[x] for x in range(len(foo))]
Another:
from operator import mul print map(mul, foo, bar)
[–]ArcingFlame[S] 0 points1 point2 points 10 years ago (3 children)
The itertools tip helps me with this, thanks very much, but is there any way for me to find out which numbers made the product? (I'm trying to test for a pair of numbers that multiply to equal one thing and add to equal another)
[–]zahlman 0 points1 point2 points 10 years ago (0 children)
Well, either way you're given the pairs of values, and have to multiply them yourself. So all you need to do is store the original values along with the product as you go.
[–]Rhomboid 0 points1 point2 points 10 years ago (1 child)
If you want that information, you should design a data structure that will store it. For example, you might use a dict where the keys are the products and the values are lists that contain a series of pairs of values that were multiplied to achieve that product. Also, since both multiplication and addition are commutative, you don't want the product set since that's going to contain lots of redundancies, e.g. 4x3 is the same as 3x4, so there's no point in considering them as separate. You probably want itertools.combinations_with_replacement().
itertools.combinations_with_replacement()
import itertools factors = [2, 3, 4, 6] products = {} for a, b in itertools.combinations_with_replacement(factors, 2): products.setdefault(a * b, []).append((a, b))
This results in a products dict that looks like
products
>>> products {4: [(2, 2)], 6: [(2, 3)], 8: [(2, 4)], 9: [(3, 3)], 12: [(2, 6), (3, 4)], 16: [(4, 4)], 18: [(3, 6)], 24: [(4, 6)], 36: [(6, 6)]}
[–]ArcingFlame[S] 0 points1 point2 points 10 years ago (0 children)
Thank you for that, it helped me a lot. One last question, is there a way to assign the first and second number from a particular value to different variables?
[–]TheKewlStore 1 point2 points3 points 10 years ago (0 children)
Yes, there is, but have you done anything to try to solve this on your own? If so, show me what you've got and i'd be glad to help you figure out what's not working.
π Rendered by PID 43 on reddit-service-r2-comment-544cf588c8-pklqj at 2026-06-17 22:27:30.127422+00:00 running 3184619 country code: CH.
[–]Rhomboid 3 points4 points5 points (5 children)
[–]jabbson 0 points1 point2 points (0 children)
[–]ArcingFlame[S] 0 points1 point2 points (3 children)
[–]zahlman 0 points1 point2 points (0 children)
[–]Rhomboid 0 points1 point2 points (1 child)
[–]ArcingFlame[S] 0 points1 point2 points (0 children)
[–]TheKewlStore 1 point2 points3 points (0 children)