Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (7.3k points)

I have a list with 15 numbers, and I need to write some code that produces all 32,768 combinations of those numbers.

I've found some code (by Googling) that apparently does what I'm looking for, but I found the code fairly opaque and am wary of using it. Plus I have a feeling there must be a more elegant solution.

The only thing that occurs to me would be to just loop through the decimal integers 1–32768 and convert those to binary, and use the binary representation as a filter to pick out the appropriate numbers.

Does anyone know of a better way? Using map(), maybe?

1 Answer

0 votes
by (106k points)

To get all possible combinations of a list’s elements you can use itertools.combinations function below is the code for the same:

itertools.combinations(iterable, r)

This function will return r length subsequences of elements from the input iterable.

Below is an example that shows how to use the itertools.combinations() function:-

import itertools

our_list = [1, 2, 3]

for L in range(0, len(our_list)+1):

    for subset in itertools.combinations(our_list, L):



Related questions

0 votes
1 answer
asked Mar 12 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories