# Limiting the number of combinations in Python

Letters are like there are 33 ones, 33 twos and 66 zeros amounting to total letters 132. If I do it manually, there will be a lot of combinations. To make it easy for computer I want to limit combinations no to 1000. Plz help.
my_letters=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

Sorry Ali123, I have no idea what you are talking about here. Your
variable letters doesnâ€™t have any letters in it, it is a list with a
single large number:

``````11111111111111111111111111
``````

There is a maximum of 1 combination that you can make by choosing
elements of a list of 1 value.

Can you please be more precise and accurate about what you are trying to
do?

Hello, @Ali123, and welcome to the Python Forum!

With limited information, all we can do is make a guess regarding what needs to be done.

Well, there are 26 occurrences of `1` here â€¦

â€¦ and your variable is named `Letters`. Is this a binary number that relates to presence or absence of each letter in a combination of letters drawn from a 26-letter alphabet?

Letâ€™s create 1000 combinations of letters, but to save space, we will display only 10 of those combinations.

``````import random
combos = []
for k in range(1000):
letters = [random.randint(0, 1) for i in range(26)]
combo = "".join([chr(i + ord("A")) for i in range(26) if letters[i] == 1 ])
combos.append(combo)
for i in range(10):
print(combos[i])
``````

Output:

``````BEFHJNXZ
ABCFGHIORUVW
BDEFKNQRSTYZ
ABCHKLMORSTUVWXY
EFKLMNOSUVWZ
CDEGHIJLMQVW
CDGHIKLMNOPRSTUVXZ
ABIJKOPQRSVXYZ
BCDEGIKMPTVWXY
``````

With the limited information we have, it seems unlikely for that to be what you are really trying to do. But it was fun doing it anyway.

Letters are like there are 33 ones, 33 twos and 66 zeros amounting to total letters 132. If I do it manually, there will be a lot of combinations. To make it easy for computer I want to limit combinations no to 1000. Plz help.
my_letters=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]

1 Like

For what you are trying to do, what qualifies as a combination? Does a combination consist of 33 ones, 33 twos and 66 zeros, but in any order?

yes, order doesnâ€™t matter. I simply want to find out 1000 different ways in which 33 ones 33 twos and 66 zeros will be arranged
Just like this [2 2 0 2 0 1 0 0 0 1 0 0 1 2 2 1 0 1 1 0 0 0 2 0 0 0 1 1 1 0 2 0 0 0 0 0 1 1 1 0 1 2 2 1 0 2 2 2 0 1 0 2 0 0 1 0 0 1 0 0 2 0 02 0 1 1 1 0 1 2 2 0 0 0 1 0 1 0 0 2 2 0 0 2 2 1 2 0 2 0 1 0 0 0 2 1 0 2 0 0 0 0 2 2 2 0 0 0 1 0 0 0 0 0 1 0 0 0 1 1 2 1 2 1 2 0 0 2 2 1 0]
this is one combination. I want 999 more of these combinations. Kindly help

1 Like

Technically, the different orders of the same numbers of each kind of object are known as permutations. Is this what is needed here?

Yes you are right. These are permutations and they are what I want to obtain

Create your `list`, then within a loop that iterates 1000 times, you can create 1000 permutations.

Please give it a try and post your code.Then we can provide additional help.

Thank you for your help

1 Like

You are very welcome.

With 1000 shuffles of your `my_letters` sequence, there is a very small chance that there will be some duplication among the resulting permutations. The likelihood of such an occurrence is extremely small, but if you need an absolute guarantee that no such duplication will occur, youâ€™ll need to take some action to prevent that. One possibility would be to sort the 1000 resulting permutations, then iterate through them to compare pairs in sequential order. If a match is found, discard and replace one of the matching permutations. Of course, even then, the replacement might match an existing permutation within your collection. There may be some more elegant solutions than this.

The odds of any of such duplication would be extremely small. You can check the references below to get a sense of how unlikely this is. Then decide whether that small likelihood is acceptable.

We hope you continue to enjoy the discussions on the Python Forum!

alternatively, to eliminate duplicates you could use the set() method.

example:

elements = [â€śabâ€ť, â€śbaâ€ť, â€śbcâ€ť, â€ścbâ€ť, â€śbaâ€ť]
len(elements)
5

len(set(elements))
4

â€¦then put your code that generates permutations of your `my_letters` sequence in a while loop that ensures each element is unique

while len(set(elements)) < 1000:
â€¦ â€¦generate more permutations
â€¦ â€¦ elements = list(set(elements))

1 Like

This is excellent, since duplicates are averted as efficiently as regarding keys within a hash table, such as a Python dictionary.

EDITED on November 26, 2021 to add â€śregarding keysâ€ť to the above phrasing.