Ask Your Question

ayyer's profile - activity

2019-07-20 04:55:21 -0600 received badge  Notable Question (source)
2018-12-20 10:01:05 -0600 received badge  Popular Question (source)
2014-09-17 07:38:01 -0600 commented question Creating new lists from a list of lists

Oh 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!)

2014-09-17 00:52:47 -0600 commented question Creating new lists from a list of lists

I would not have expected these additional comma's. In the addendum example, I would have liked to have [(1), (2)]

2014-09-16 07:23:59 -0600 received badge  Editor (source)
2014-09-03 03:52:34 -0600 commented answer Creating new lists from a list of lists

Thanks, that's perfect!

2014-09-03 03:51:51 -0600 received badge  Scholar (source)
2014-09-02 03:50:38 -0600 asked a question 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?