36

I have this simple piece of code that returns what's in the title. Why doesn't the array simply print? This is not just an itertools issue I've also noticed it for other code where it'll just return the object location.

Here is the code. I'm running 2.7.1, an enthought distribution (pylab) - using it for class.

import itertools

number = [53, 64, 68, 71, 77, 82, 85]

print itertools.combinations(number, 4)
Julian E.
  • 4,687
  • 6
  • 32
  • 49
tshauck
  • 20,746
  • 8
  • 36
  • 36
  • 1
    I've tagged your question as 'homework' because you mentioned 'using it for a class' -- let me know if that's actually not true for this particular question. – phooji Mar 03 '11 at 03:19

3 Answers3

50

It doesn't print a simple list because the returned object is not a list. Apply the list function on it if you really need a list.

print list(itertools.combinations(number, 4))

itertools.combinations returns an iterator. An iterator is something that you can apply for on. Usually, elements of an iterator is computed as soon as you fetch it, so there is no penalty of copying all the content to memory, unlike a list.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Is there like a .next() function to get the next element ? without using a for loop, I don't want to copy the whole thing to memory – IssamLaradji Nov 30 '17 at 15:52
  • @Curious Yes, call `next(c)` where `c` is the combinations iterator. – kennytm Nov 30 '17 at 17:07
  • Thanks @kennytm, is it correct to assume that I can't access the 10th index unless I call `next` 10 times ? – IssamLaradji Dec 01 '17 at 00:12
  • 3
    @Curious You could use `itertools.islice` to jump to the 10th index (check the `nth` recipe in https://docs.python.org/3/library/itertools.html). – kennytm Dec 01 '17 at 08:15
11

Try this:

for x in itertools.combinations(number, 4):
   print x

Or shorter:

results = [x for x in itertools.combinations(number, 4) ]

Basically, all of the itertools module functions return this type of object. The idea is that, rather than computing a list of answers up front, they return an iterable object that 'knows' how to compute the answers, but doesn't do so unless `asked.' This way, there is no significant up front cost for computing elements. See also this very good introduction to generators.

phooji
  • 10,086
  • 2
  • 38
  • 45
2

Beside the proposed solutions, other ways, with a little change to your code, you can do:

1)

import itertools

number = [53, 64, 68, 71, 77, 82, 85]

res = itertools.combinations(number, 4)

print(*res)

Put the result of the combination into a variable (res), after, with print, use a single asterisks * to unpacks the sequence/collection into positional arguments. More detail is in here.

2)

import itertools

number = [53, 64, 68, 71, 77, 82, 85]

*res, = itertools.combinations(number, 4)

print(res)

This in python 3, it called "Extended Iterable Unpacking"

A good tutorials about using Asterisks (*, **) in here.

ibra
  • 1,164
  • 1
  • 11
  • 26