Python – Use .join() in Python to color all columns in an array except the first column

Use .join() in Python to color all columns in an array except the first column… here is a solution to the problem.

Use .join() in Python to color all columns in an array except the first column

I’m trying to color all the elements of the array after the first column. I finally got it working, but I knew there were more “pythonic” ways to do this and looked for some suggestions:

for row in board:    
    print row[0] + " " + " ".join(colored(element, element_colors[element])
                                  for element in row[1:])

Any suggestions on how to do this in a more pythonic way would be appreciated!

Edit:
The expected output is as follows:

[1] [W] [W] [W] [W] [W] [W] [W] [W]
[2] [W] [W] [W] [W] [W] [W] [W] [W]
[3] [W] [W] [W] [W] [W] [W] [W] [W]
[4] [W] [W] [W] [W] [W] [W] [W] [W]
[5] [W] [W] [W] [W] [W] [W] [W] [W]
[6] [W] [W] [W] [W] [W] [W] [W] [W]
[7] [W] [W] [W] [W] [W] [W] [W] [W]
[8] [W] [W] [W] [W] [W] [W] [W] [W]

Where 1-8 are not colored by shaded functions, and all [W] spaces are.

So I can make the elements_color dictionary smaller :

element_colors = {'[X]': 'red', '[H]': 'magenta', '[W]': 'cyan'}

Solution

Another way is to glue them together using +

' '.join([row[0]] + [colored(e, element_colors[e]) for e in row[1:]])

Related Problems and Solutions