This question already has an answer here:
The viceroy butterfly generates a toxin compound which make it distasteful to predators. Biologists agree that the viceroy must have developed this trait as a passive defence mechanism to prevent being eaten.
Let’s look at it from an information point of view.
For this to work, information about the bad taste of the viceroy must be known to the predators. This can happen by a predator eating one viceroy, discovering the bad taste, learning and never doing it again. Unfortunately, the unpalatable butterfly had to die in the process.
The second, and most important, information path is the genetic one. One way, evolution work is by the survival of the fittests. In this case the important trait of “tasting bad” dies with the butterfly and is less likely to have been transmitted to a new generation, unless (and this might be part of the answer) we consider the local population of butterfly as the evolving organism.
The question: If a successful gene is only proven successful when removed from the gene pool, how does the species evolve a trait involving this gene?