from colorama import Fore, init
init()
key_numbers = [1,3,5,7,9]
numbers = [3,4,6,3,8,9,7,9,3,1]
for number in numbers:
if number in key_numbers:
number1 = Fore.RED + number
numbers = [number1 if number else number for number in numbers]
else:
continue
Essentially I want the code to go through each number in the numbers list and check whether the respective number exists in the key_numbers list.
If it exists, I want to replace the number with red font and move on to the next number
The output should have a list with numbers highlighted in red if they exist in the key_numbers list and the other numbers in regular font and color.
I think I am going wrong trying to replace the numbers with number1. Can someone please help me where I am going wrong?
You can check out the
mapoperator:You don't need to provide a lambda. You can also explicitly define a Function that returns a red number if it is in keys and otherwise a black one.
E.g.