0

I have a problem like this. I have two lists, A and B, where A=[[1,2],[3,4],[5,6]] and B=[["a","b"],["c","d"]], I would like to got a new list from these two like

C = [
     [[1,2],["a","b"]],
     [[3,4],["a","b"]],
     [[1,2],["c","d"]],
     [[3,4],["c","d"]]
    ]

I had try the following code:

A = [[1,2],[3,4]]
B=[["a","b"],["c","d"]]
for each in A:
    for evey in B:
        print each.append(evey)

However, the output is None.

Any helpful information are appreciated. Thank you.

By the way, I had try to replace the "append" with simple "+". The output is a list which elements are not list.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241

5 Answers5

2

This was answered here: Get the cartesian product of a series of lists?

Try this:

import itertools

A = [[1,2],[3,4]]
B = [["a","b"],["c","d"]]
C = []

for element in itertools.product(A,B):
    C.append(list(element))

print C
Community
  • 1
  • 1
  • If you think the question is a duplicate, vote to close it. –  Jun 15 '15 at 07:02
  • Since it is a duplicate, then why you answer it? – Mp0int Jun 15 '15 at 07:09
  • @FallenAngel: I don't have enough points to vote it as duplicate (and I'm new here). – Sorin Negulescu Jun 15 '15 at 07:13
  • Welcome, it was all documented in the help files. In [here](http://stackoverflow.com/help/flagging) and [here](http://stackoverflow.com/help/duplicates) you can find related parts. It is not expected from the new comers to read all the help section, but it will be great to check there and learn the basics of the site. – Mp0int Jun 15 '15 at 07:23
  • Thank you. I will make use of that links! Also, the OP might have not known that this is actually a Cartesian product related question... – Sorin Negulescu Jun 15 '15 at 07:28
1

This is one way to do it:

A = [[1,2],[3,4]]
B=[["a","b"],["c","d"]]
C = zip(A,B)

The output here is a list of tuples:

[([[1, 2], [3, 4]],), ([['a', 'b'], ['c', 'd']],)]

If you want a list of lists, you can do this:

D = [list(i) for i in zip(A, B)]

The output:

[[[1, 2], ['a', 'b']], [[3, 4], ['c', 'd']]]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • 1
    zip function doesn't solve the problem. It shows `[([1, 2], ['a', 'b']), ([3, 4], ['c', 'd'])]` – vasilenicusor Jun 15 '15 at 07:13
  • Thanks for your help. As @vasilenicusor said it may need a iteration to produce the result I want. Actually, I find another very simple way to solve it. Just using : C= [x,y, for x in A for y in B] – Thomas Ding Jun 15 '15 at 07:14
  • @ThomasDing it seems that the second output in my answer `D` which is this `[[[1, 2], ['a', 'b']], [[3, 4], ['c', 'd']]]` is what you wanted. BTW: have you tested your solution? – Joe T. Boka Jun 15 '15 at 07:25
0

Try this. You have to append each couple of elements in each iteration.

result = []
for each in A:
    for evey in B:
        result.append([each,evey])
>>>result
[[[1, 2], ['a', 'b']],
 [[1, 2], ['c', 'd']],
 [[3, 4], ['a', 'b']],
 [[3, 4], ['c', 'd']]]

OR

simply use itertools.product

>>>from itertools import product   
>>>list(product(A,B))
[([1, 2], ['a', 'b']),
 ([1, 2], ['c', 'd']),
 ([3, 4], ['a', 'b']),
 ([3, 4], ['c', 'd'])]    
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

Don't print return value of append(), try this:

A = [[1,2],[3,4]] B=[["a","b"],["c","d"]] C = [] for each in B: for evey in A: C.append([evey, each]) print C

maliku
  • 1
  • 1
0

You can use itertools.product to achieve this.

import itertools

list(itertools.product(A,B))  # gives the desired result    
[([1, 2], ['a', 'b']),
 ([1, 2], ['c', 'd']),
 ([3, 4], ['a', 'b']),
 ([3, 4], ['c', 'd']),
 ([5, 6], ['a', 'b']),
 ([5, 6], ['c', 'd'])]

itertools.product(*iterables[, repeat])

It returns the Cartesian product of input iterables
Eg.
product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126