Home > Software design >  colour element of a list by looping over another list
colour element of a list by looping over another list

Time:03-12

I want to write a function in python that will take a list B and loop over another list A. if an item in list B is present in list A, it colours it red else ignores it and returns a list A with both coloured and uncoloured text. my code isn't working at the moment.

A = ["a", "b", "c", "c"] B = ["a" "c"]

def color() for i in B: if i in A: print(Fore.RED i)

color()

CodePudding user response:

I think you can use one loop for this.

class colors:
    RED = '\033[31m'
    ENDC = '\033[m'

A = ["a", "b", "c", "d"]
B = ["a", "c"]

def color():
    for i in A:
        if i in B:
            print(colors.RED   i   colors.ENDC   " ", end="")
        else:
            print(i   " ", end="")
color()

CodePudding user response:

You're looking for something like this?

A = ["a", "b", "c", "c"] 
B = ["a" "c"]
def color():
    for i in B: 
        if i in A: 
            A.append(Fore.Red i)
    print(A)
color()
  • Related