How do I color text inside of a list?

When I print the line it does work, but when I try it inside the list it just prints the color code. How can I make it colored inside the list?

Hello @csunesen83!

When printing a raw list in Python, the color codes automatically get hidden. However, you can use the snippet of code below to print a list.

def printlist(input_list):
  overall = "["
  for i in range(len(input_list)):
    t="," if  i+1 != len(input_list) else ""
    overall += str(input_list[i]) + t
  overall += "]"

And then you can use it by:

mylist = [1,4,2]

Hope this helps!

Hello, that code is highly inefficient.
Here’s an improved but equivalent function:

def print_list(input_list):
  print(f"[{', '.join(input_list)}]")

You can remove the brackets or change the separator too.

1 Like