How to get all combination of n binary value?

55,668

Solution 1

Use itertools.product

import itertools
lst = list(itertools.product([0, 1], repeat=3))

This will yield a list of tuples (see here)

You can easily change this to use a variable repeat:

n = 3
lst = list(itertools.product([0, 1], repeat=n))

If you need a list of lists, then you can use the map function (thanks @Aesthete).

lst = map(list, itertools.product([0, 1], repeat=n))

Or in Python 3:

lst = list(map(list, itertools.product([0, 1], repeat=n)))
# OR
lst = [list(i) for i in itertools.product([0, 1], repeat=n)]

Note that using map or a list comprehension means you don't need to convert the product into a list, as it will iterate through the itertools.product object and produce a list.

Solution 2

Without using any in-build functions or smart techniques we can get like this.

def per(n):
    for i in range(1<<n):
        s=bin(i)[2:]
        s='0'*(n-len(s))+s
        print (map(int,list(s)))
per(3)       

output

[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]

Solution 3

Following will give you all such combinations

bin = [0,1]
[ (x,y,z) for x in bin for y in bin for z in bin ]
Share:
55,668
LWZ
Author by

LWZ

I'm learning Python.

Updated on July 09, 2022

Comments

  • LWZ
    LWZ almost 2 years

    In Python, how can I get all combinations of n binary values 0 and 1?

    For example, if n = 3, I want to have

    [ [0,0,0], [0,0,1], [0,1,0], [0,1,1], ... [1,1,1] ]  #total 2^3 combinations
    

    How can I do this?

  • eumiro
    eumiro about 11 years
    He gives n=3 as an example only, so he wants to have this parametrized.
  • LWZ
    LWZ about 11 years
    This works but n can't be large...
  • Aesthete
    Aesthete about 11 years
    +1 - map(list, product([0, 1], repeat=3)) will return the same format in case the OP is interested.
  • LWZ
    LWZ about 11 years
    This is great, tuple is ok too.
  • LWZ
    LWZ about 11 years
    @Volatility, just of curiosity, how do you know this? I had no clue how to find this function from the Python documentation.
  • Volatility
    Volatility about 11 years
    @LWZ It comes with experience. (see point 6)
  • LWZ
    LWZ about 11 years
    @Volatility, what a great post, thanks!
  • user1111929
    user1111929 about 5 years
    For me, this yields: <map object at 0x000001B30C760128> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048> <map object at 0x000001B30C760048>
  • armitus
    armitus about 5 years
    Thank you! This was extremely helpful
  • FReeze FRancis
    FReeze FRancis about 5 years
    This wont scale!
  • Aditya Lahiri
    Aditya Lahiri almost 5 years
    @Volatility thank you for the solution, I am trying to the same thing with 26 variables but I think my Jupyter NB runs out of memory. Do you know how I can possibly tackle this issue. Thanks.
  • WattsInABox
    WattsInABox over 4 years
    Have to call (list(map...)) to get actual lists out
  • Richard Li
    Richard Li over 3 years
    Great idea! here is a simple version with format function: ``` def per(n): for i in range(1<<n): s="{:03b}".format(i) print(list(map(int, list(s)))) >>> per(3)
  • Jed
    Jed about 3 years
    @aditya lahiri the array size will be 2^n x n. For 26 that's quite a long array.