1

Consider the following code below:

list1 = ['1x', '2x']
list2 = ['x18', 'x74']
list3 = [('100p1', '100p2'), ('300p1', '300p2')]

gen_list = [[a,b] for a in list1 for b in list2]

for new_list in gen_list:
    for c in list3:
        print(new_list.extend(c))

My target result is like this:

[['1x','x18, '100p1', '100p2'],
 ['1x','x74, '100p1', '100p2'],
 ['1x','x18, '300p1', '300p2'],
 ['1x','x74, '300p1', '300p2'],
 ['2x','x18, '100p1', '100p2'],   
 ['2x','x74, '100p1', '100p2'],
 ['2x','x18, '300p1', '300p2'],
 ['2x','x74, '300p1', '300p2']]

but the result of the above code is this:

None
None
None
None
None
None
None
None

What necessary correction do I need to apply on my code? Thanks in advance.

1
  • 4
    print(new_list.extend(c)) prints None because extends returns None. Just print new_listafter the loops. Commented Jul 5, 2017 at 9:16

1 Answer 1

3

using itertools.product, unpacking and a list comprehension

[[l[0], l[2], *l[1]] for l in itertools.product(list1, list3, list2)]

or

[[l1, l2, *l3] for l1, l3, l2 in itertools.product(list1, list3, list2)]

before Python 3.5

For versions before Python 3.5 you could do something like this

[[l1, l2] + list(l3) for l1, l3, l2 in itertools.product(list1, list3, list2)]

If you know l3 only contains 2 elements you can use nested unpacking, as mentioned by @ShadowRanger

[[a, b, c1, c2] for a, (c1, c2), b in itertools.product(list1, list3, list2)]
Sign up to request clarification or add additional context in comments.

5 Comments

I tried this line here but still returns an syntax error like this: Syntax Error: can use starred expression only as assignment target My system is in python 3.4. I don't really know why this is a syntax error.
It's a new feature in 3.5 docs.python.org/3/whatsnew/… I 'll see for a way that works in 3.4 when I'm at my pc
@MaartenFabré: Note: To get the exact ordering desired by the OP, you need to change the argument ordering for itertools.product from list1, list2, list3 to list1, list3, list2, so that the value derived from list2 is alternating most rapidly; similarly, you'd change l1, l2, l3 to l1, l3, l2 so unpacking matches. Demo (w/plain loop to match OP's code)
@MaartenFabré: Also, given list3 is a list of fixed length tuples, even pre-3.5 you can avoid creating/concatenating temporary lists by using nested unpacking syntax; it's faster, as it avoids the generic function call lookup and dispatch required by the list constructor: [[a, b, c1, c2] for a, (c1, c2), b in itertools.product(list1, list3, list2)] (Try it online!)
Thanks for the effort. I actually used the one suggested by @ShadowRanger as it is simple and straightforward, not worrying about using the * or + operators for I am not well-trained to use those techniques. Though the explanations are good and informative. I might as well include it on my personal knowledge base. Thanks everyone!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.