ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 17 Sep 2014 09:38:06 -0500Creating new lists from a list of listshttps://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/I have a newbie question about list comprehension. Suppose I have a list of lists as follows.
A = [[1, 2], [3], [4, 5]]
I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:
[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?
Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.
sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
I am taking care of this separately right now, but is there a way to include this case too?Tue, 02 Sep 2014 03:50:38 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/Comment by slelievre for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24165#post-id-24165Or maybe it's (1, 2, 3) which looks incomplete. In Python you can also enter it as (1, 2, 3,) but in the output it's displayed in the usual "incomplete" form (1, 2, 3).Wed, 17 Sep 2014 09:38:06 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24165#post-id-24165Comment by ayyer for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24164#post-id-24164Oh I see! I had an error in my program in this case, but that was probably for some other reason. Thanks for the clarification. (The hanging comma's make the expression look strangely incomplete though!)Wed, 17 Sep 2014 07:38:01 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24164#post-id-24164Comment by slelievre for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24161#post-id-24161In Python, (1) is the same as 1, and a 1-tuple is entered and displayed with a comma. This makes sense and allows to distinguish for example between (3+2)*4 and (3+2,)*4 (try those in Sage).Wed, 17 Sep 2014 04:02:53 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24161#post-id-24161Comment by ayyer for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24160#post-id-24160I would not have expected these additional comma's. In the addendum example, I would have liked to have
[(1), (2)]Wed, 17 Sep 2014 00:52:47 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24160#post-id-24160Comment by slelievre for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24154#post-id-24154About your addendum: to me, your new example seems to show that it also works for a list of length one. What result did you expect?Tue, 16 Sep 2014 13:24:55 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24154#post-id-24154Answer by slelievre for <p>I have a newbie question about list comprehension. Suppose I have a list of lists as follows.</p>
<pre><code>A = [[1, 2], [3], [4, 5]]
</code></pre>
<p>I want to create all possible lists by choosing exactly one element from each list. Is there a quick way of doing that? In other words, I want the program to return the following for A:</p>
<pre><code>[[1,3,4],[1,3,5],[2,3,4],[2,3,5]]
</code></pre>
<p>I can do this if the length of A is 3, for instance. But I want to include this in a program where the length of the list A is an input and the sublists are created from the input. Is there a quick way to do this?</p>
<p>Addendum:
I just realised that this doesn't work when A is a list of length 1. See the example below.</p>
<pre><code>sage: A = [[1, 2]]
sage: list(itl.product(*A))
[(1,), (2,)]
</code></pre>
<p>I am taking care of this separately right now, but is there a way to include this case too?</p>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?answer=24020#post-id-24020You can use itertools.product, as follows:
sage: A = [[1, 2], [3], [4, 5]]
sage: import itertools as itl
sage: list(itl.product(*A))
[(1, 3, 4), (1, 3, 5), (2, 3, 4), (2, 3, 5)]
Tue, 02 Sep 2014 04:44:12 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?answer=24020#post-id-24020Comment by ayyer for <p>You can use itertools.product, as follows:</p>
<pre><code>sage: A = [[1, 2], [3], [4, 5]]
sage: import itertools as itl
sage: list(itl.product(*A))
[(1, 3, 4), (1, 3, 5), (2, 3, 4), (2, 3, 5)]
</code></pre>
https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24029#post-id-24029Thanks, that's perfect!Wed, 03 Sep 2014 03:52:34 -0500https://ask.sagemath.org/question/24019/creating-new-lists-from-a-list-of-lists/?comment=24029#post-id-24029