I was reading this article
Click Here
http://maxburstein.com/blog/python-shortcuts-for-the-python-beginner/

In there was this example

from itertools import combinations
teams = ["Packers", "49ers", "Ravens", "Patriots"]
for game in combinations(teams, 2):
    print game

>>> ('Packers', '49ers')
>>> ('Packers', 'Ravens')
>>> ('Packers', 'Patriots')
>>> ('49ers', 'Ravens')
>>> ('49ers', 'Patriots')
>>> ('Ravens', 'Patriots')

So I thought I would play with it. very simply just used numbers to create a bigger list and a bigger tuple.

>>> from itertools import combinations
>>> numbers = [1,2,3,4,5,6,7,8,9,10]
>>> for game in combinations(numbers, 6):
    print game

When I received more combinations than I expected I wondered how many combinations had I received.

>>> len(game)
6

So length only returns the length of one game. How do I find out how many tuples were created by combinations.

Use len(list(combinations(numbers, 6)))

Ok, hmmm so you can't get the information from 'game' directly have to call Len on the combinations function.

So its all because combinations doesn't group its tuples into a main tuple. Otherwise it would work.

>>> myTup = ((1,2),(2,3),(3,4))
>>> len(myTup)
3

Ok, hmmm so you can't get the information from 'game' directly have to call Len on the combinations function.

You don't call len on the combination function, you call len on the list function, which returns a list with all the combinations. To make it more clear len(list(combinations(numbers, 6))) is like this:

>>> numbers = [0,1,2,3,4,5,6,7,8,9]
>>> l = []
>>> for i in combinations(numbers, 6): l.append(i)
>>> len(l)

Having as output:

'''
210
'''

You need to check the effects of sample size.

In other words, game in your code is just one combination. To get a list of all combinations you have to append to an empty list (as shown by Lucaci Andrew) or use the list(combinations()) approach (shown by Griboullis) ...

from itertools import combinations

numbers = [1,2,3,4,5,6,7,8,9,10]
games = []
for game in combinations(numbers, 6):
    print game
    games.append(game)

# length of last game tuple
print len(game)   # 6
# length of list of all game combinations
print len(games)  # 210

# or

games2 = list(combinations(numbers, 6))
print len(games2)  # 210
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.