### Thread: Multiplying Elements of Lists Together

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
3
Rep Power
0

#### Multiplying Elements of Lists Together

Hello Everyone,

I'm trying to write a program to help me understand a game with very complicated probability involved. Because the probability is so complicated, I'm trying to "brute force" the game by using a program to calculate every possible outcome for me.

I'm trying to multiply all the elements of one list to all the elements of another list and form a new list from it. For example:

Let a,b,c be some numbers. (NOT STRINGS! They're numbers, as in floats. I'm only using a,b,c to make the desired output more clear.)

List1 = [1,2,3]
List2 = [a,b,c]

Desired Output = [1a,2a,3a,1b,2b,3b,1c,2c,3c]

I'm a newby with Python. I've only been studying it for about 2.5 weeks. I tried using for loops, but that's posing an issue because I believe I'd need to loop through two lists with the same line of code. As far as I know, that's not possible. Putting a for loop inside of another won't work, unless I'm mistaken? I decided to try combining two for loops as follows. Example:

Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = []
list3.append(m*i for m in list1 for i in list2)
print list3```
The resulting output is:

Code:
`[<generator object <genexpr> at 0x021D5990>]`
That's not an error. That's the actual output. I have absolutely no idea what it means but it's certainly not a list like I want!

Thanks in advance for any and all help. Remember, I'm a newby! I don't know much yet. D:
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
3
Rep Power
0
I also tried the following:

Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = []
for i in list1:
list3.append(m*i for m in list2)
print list3```
This resulted in:

Code:
`[<generator object <genexpr> at 0x021D5990>, <generator object <genexpr> at 0x021D5BE8>, <generator object <genexpr> at 0x021D5C38>]`
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
438
Rep Power
67
This is where zip() comes in handy.

Code:
`list3 = [a * b for a, b in zip(list1, list2)]`
Correction: Oops, sorry, read you wrong. What you want cannot – as far as I can see – to be solved with list comprehensions, so:

Code:
```list3 = []
for a in list1:
for b in list2:
list3.append(a * b)```
Last edited by SuperOscar; November 25th, 2012 at 04:26 AM.
4. Actually you were on the right track. You didn't turn the generator object into a list with list():
Code:
```list1 = [1,2,3]
list2 = [4,5,6]
list3 = list(m*i for m in list1 for i in list2)
print list3

'''my result >>>
[4, 5, 6, 8, 10, 12, 12, 15, 18]
'''```
5. Cartesian product is the j verb { .
Code:
```   { 'abcd' ; '012345'
┌──┬──┬──┬──┬──┬──┐
│a0│a1│a2│a3│a4│a5│
├──┼──┼──┼──┼──┼──┤
│b0│b1│b2│b3│b4│b5│
├──┼──┼──┼──┼──┼──┤
│c0│c1│c2│c3│c4│c5│
├──┼──┼──┼──┼──┼──┤
│d0│d1│d2│d3│d4│d5│
└──┴──┴──┴──┴──┴──┘

{ 'ab' ; '012' ; 'ABCD'
┌───┬───┬───┬───┐
│a0A│a0B│a0C│a0D│
├───┼───┼───┼───┤
│a1A│a1B│a1C│a1D│
├───┼───┼───┼───┤
│a2A│a2B│a2C│a2D│
└───┴───┴───┴───┘

┌───┬───┬───┬───┐
│b0A│b0B│b0C│b0D│
├───┼───┼───┼───┤
│b1A│b1B│b1C│b1D│
├───┼───┼───┼───┤
│b2A│b2B│b2C│b2D│
└───┴───┴───┴───┘

1 2 3 */ 4 5 6   NB. table, or outer product
4  5  6
8 10 12
12 15 18

, 1 2 3 */ 4 5 6
4 5 6 8 10 12 12 15 18```
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
3
Rep Power
0
Thank you so much everyone!

Now all I have to do is figure out what exactly is double counted and what isn't for some of the more complicated parts of this program. Then I can finish this up.

You guys rock.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
438
Rep Power
67
Originally Posted by Dietrich
Code:
`list3 = list(m*i for m in list1 for i in list2)`
Oh, I stand corrected.