Ask Your Question

ayyer's profile - activity

2021-01-21 11:45:53 +0200 received badge  Famous Question (source)
2019-07-20 11:55:21 +0200 received badge  Notable Question (source)
2018-12-20 17:01:05 +0200 received badge  Popular Question (source)
2014-09-17 14:38:01 +0200 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 07:52:47 +0200 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 14:23:59 +0200 received badge  Editor (source)
2014-09-03 10:52:34 +0200 commented answer Creating new lists from a list of lists

Thanks, that's perfect!

2014-09-03 10:51:51 +0200 received badge  Scholar (source)
2014-09-02 10:50:38 +0200 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?