8

When I tried running the following code

product([1,2,3],['a','b'])

it returned an object of the following type:

<itertools.product object at 0x01E76D78>

Then I tried calling list() on it and I got the following:

[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b')]

Is there any way to make it into a list of lists, instead of tuples? Like this:

[[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b'], [3, 'a'], [3, 'b']]

Thanks in advance!

Hari
  • 1,561
  • 4
  • 17
  • 26
turtlesoup
  • 3,188
  • 10
  • 36
  • 50
  • 1
    It returns the cartesian product. – Diego Navarro Jun 19 '12 at 06:51
  • 2
    What is your question? Are you wondering what `itertools.product` returns? Or are you wondering how to convert the return value into a list of lists? – David Wolever Jun 19 '12 at 06:51
  • 1
    Why is it you need to do this? It seems very likely that whatever you are trying to achieve can be done in a better way. – lvc Jun 19 '12 at 06:57
  • 2
    `itertools` are there to avoid building lists. – eumiro Jun 19 '12 at 06:58
  • Are lists worse than tuples? I was trying to convert tuples into lists because I need to format them for something else. I want to know both what itertools.product returns, and how to convert them into lists. Sorry for the ambiguity – turtlesoup Jun 19 '12 at 07:54
  • A further question to the above asked question. how does one call each element from the returned value in the above-asked question? – Sahas Nov 16 '18 at 17:34

3 Answers3

8
list(map(list, product([1, 2, 3], ['a', 'b'])))

The outermost list() is not necessary on Python 2, though it is for Python 3. That is because in Python 3 a map object is returned by map().

Hari
  • 1,561
  • 4
  • 17
  • 26
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 2
    This is why I think a list comprehension is better for this being cross-compatible although it is a very odd task. – jamylak Jun 19 '12 at 07:14
  • @jamylak: Yes, it'd indeed be more cross-compatible. The reason I avoided them is that I generally avoid them if I don't actually *need* their dummy variables, to avoid introducing noise. (`map(list, ...)` reads a lot better to me than `[list(l) for l in ...]`... YMMV) – user541686 Jun 19 '12 at 07:48
  • @Mehrad I see your point but in this case your solution would look better on Python 2 but worse in Python 3 – jamylak Jun 19 '12 at 07:52
  • @jamylak: I guess I missed replying to this 3 years ago, but the problem is that in Python 2 the dummy variable can affect the rest of the program since its scope leaks. It's a correctness thing not just an aesthetic thing. – user541686 Jul 17 '15 at 20:33
  • What are you referring to? – jamylak Jul 19 '15 at 08:46
  • @jamyla: I'm referring to your last reply; I'm saying that it's more than a readability issue; in Python 2 the list comprehension's dummy variable can reassign another variable of the same name in the current function. – user541686 Jul 19 '15 at 09:28
  • Then I don't agree with you because that applies to every use of a list comprehension. You're basically saying don't ever use a list comprehension because you may accidentally use the same variable name which will leak into the scope. Most people will just use a different descriptive name for everything anyway. This is not a valid reason to avoid using list comprehensions in Python 2 – jamylak Jul 19 '15 at 20:46
  • @jamylak: That's exactly what I'm saying, yeah. :P I think it is but it's okay haha. – user541686 Jul 19 '15 at 21:50
2

It returns an iterator of tuples. If you want a list of lists, you can do:

[list(a) for a in product([1,2,3],['a','b'])]

However, in many cases, just using the iterator is better. You can just do:

for item in product([1,2,3],['a','b']):
    # do what you want with the item
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

You can do the following but it is pointless. Since the values do not change a tuple is more suited to this than a list. It also saves memory.

>>> from itertools import product
>>> [list(x) for x in product([1,2,3],['a','b'])]
[[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b'], [3, 'a'], [3, 'b']]
jamylak
  • 128,818
  • 30
  • 231
  • 230