{"record":{"id":"ddc58074271a07e9","repo":"TheAlgorithms/Python","slug":"not-in-genes-list-is-not-in-genes-list-evolutio","errorCode":null,"errorMessage":"{not_in_genes_list} is not in genes list, evolution cannot converge","messagePattern":"(.+?) is not in genes list, evolution cannot converge","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"genetic_algorithm/basic_string.py","lineNumber":130,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: ['e', 's'] is not in genes list, evolution cannot converge\n    >>> genes.remove(\"t\")\n    >>> basic(\"test\", genes)\n    Traceback (most recent call last):\n        ...\n    ValueError: ['e', 's', 't'] is not in genes list, evolution cannot converge\n    \"\"\"\n\n    # Verify if N_POPULATION is bigger than N_SELECTED\n    if N_POPULATION < N_SELECTED:\n        msg = f\"{N_POPULATION} must be bigger than {N_SELECTED}\"\n        raise ValueError(msg)\n    # Verify that the target contains no genes besides the ones inside genes variable.\n    not_in_genes_list = sorted({c for c in target if c not in genes})\n    if not_in_genes_list:\n        msg = f\"{not_in_genes_list} is not in genes list, evolution cannot converge\"\n        raise ValueError(msg)\n\n    # Generate random starting population.\n    population = []\n    for _ in range(N_POPULATION):\n        population.append(\"\".join([random.choice(genes) for i in range(len(target))]))\n\n    # Just some logs to know what the algorithms is doing.\n    generation, total_population = 0, 0\n\n    # This loop will end when we find a perfect match for our target.\n    while True:\n        generation += 1\n        total_population += len(population)\n\n        # Random population created. Now it's time to evaluate.\n\n        # (Option 1) Adding a bit of concurrency can make everything faster,\n        #","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/genetic_algorithm/basic_string.py#L112-L148","documentation":"Raised by basic() in genetic_algorithm/basic_string.py when the target string contains characters absent from the genes list. The algorithm builds every candidate solely from random.choice(genes); if the target needs a character the population can never produce, mutation can never converge and the loop would run forever — so the function aborts with the exact missing characters in the message.","triggerScenarios":"Calling basic('test', genes) after genes.remove('t') — the message lists ['e', 's', 't'] or the subset actually missing. Any target/genes mismatch triggers it: digits in the target with letter-only genes, accented characters, or a space not included in genes.","commonSituations":"Building genes from string.ascii_letters but targeting a string containing digits, spaces, or punctuation; locale-specific characters (accented letters) missing from ASCII gene sets; whitespace stripped from a gene string by overzealous cleanup.","solutions":["Include every character of the target in genes, e.g. genes = set(ascii_letters) | set(target) or a charset known to cover the target.","Or sanitize the target to the gene alphabet before calling.","Check the message — it names exactly which characters are missing."],"exampleFix":"# before\nimport string\nbasic('hello world 42', string.ascii_letters)  # space and digits missing\n\n# after\ngenes = set(string.ascii_letters + string.digits + ' ')\nbasic('hello world 42', genes)","handlingStrategy":"validation","validationCode":"missing = {c for c in target if c not in genes}\nif missing:\n    raise ValueError(f'target uses characters outside genes: {sorted(missing)}')\nbasic(target, genes)","typeGuard":"def target_is_reachable(target: str, genes) -> bool:\n    return all(c in genes for c in target)","tryCatchPattern":"try:\n    basic(target, genes)\nexcept ValueError as exc:\n    if 'not in genes list' in str(exc):\n        genes = set(genes) | set(target)  # widen gene pool\n        basic(target, genes)\n    else:\n        raise","preventionTips":["Derive the gene set from the target alphabet, not the other way around.","Test with target strings that exercise every character class you support."],"tags":["genetic-algorithm","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}