Hallvard
Hallvard

Reputation: 201

Listing all permutations of RGB in Python

I am trying to make a list of all possible 27 3-character permutations of RGB. My function is as follows:

def make_big_RGB():
    rgb = ('r', 'g', 'b')
    comb_list = []
    current_combination = list(rgb) #start with combination (r, g, b)
    for x in range(3):
        current_combination[0] = rgb[x]
        for y in range(3):
            current_combination[1] = rgb[y]
            for z in range(3):
                current_combination[2] = rgb[z]
                comb_list.append(current_combination)
                print('Added' + str(current_combination))
    for _ in comb_list:
        print(_)
make_big_RGB()

The result is as follows:

Added['r', 'r', 'r']
Added['r', 'r', 'g']
Added['r', 'r', 'b']
Added['r', 'g', 'r']
Added['r', 'g', 'g']
Added['r', 'g', 'b']
Added['r', 'b', 'r']
Added['r', 'b', 'g']
Added['r', 'b', 'b']
Added['g', 'r', 'r']
Added['g', 'r', 'g']
Added['g', 'r', 'b']
Added['g', 'g', 'r']
Added['g', 'g', 'g']
Added['g', 'g', 'b']
Added['g', 'b', 'r']
Added['g', 'b', 'g']
Added['g', 'b', 'b']
Added['b', 'r', 'r']
Added['b', 'r', 'g']
Added['b', 'r', 'b']
Added['b', 'g', 'r']
Added['b', 'g', 'g']
Added['b', 'g', 'b']
Added['b', 'b', 'r']
Added['b', 'b', 'g']
Added['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']

The last for loop prints out the wanted result, but when I subsequently try to print the whole list, the result is somehow a list of 27 items of [b, b, b]. I don't understand why.

Upvotes: 0

Views: 389

Answers (3)

jpp
jpp

Reputation: 164733

This should work:

from itertools import product

list(product('rgb', repeat=3))

Upvotes: 4

Gab
Gab

Reputation: 530

One Solution:

import itertools

result = itertools.product("rgb",repeat = 3)    
print(list(result))

Upvotes: 0

tobias_k
tobias_k

Reputation: 82929

The problem is that with

comb_list.append(current_combination)

you are appending the same list again and again to the result list. Thus changing a value in that list, e.g. with current_combination[0] = rgb[x] will also change the value in all the "other" lists.

You could fix this by creating a new list from that list prior to insertion:

comb_list.append(list(current_combination))

Or just create that list from the three elements directly when adding it to the result:

for x in rgb:
    for y in rgb:
        for z in rgb:
            comb_list.append([x, y, z])

Or just use a list-comprehension for the whole thing:

comb_list = [[x, y, z] for x in rgb for y in rgb for z in rgb]

Or use itertools.product, as described in the other answer.

Upvotes: 1

Related Questions