{"record":{"id":"fb305bd7c46f5747","repo":"jax-ml/jax","slug":"condlist-must-have-length-equal-to-choicelist","errorCode":null,"errorMessage":"condlist must have length equal to choicelist ({} vs {})","messagePattern":"condlist must have length equal to choicelist \\((.+?) vs (.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":2864,"sourceCode":"    Array([ 10,   2, 300,   0], dtype=int32)\n\n    This is logically equivalent to the following nested ``where`` statement:\n\n    >>> default = 0\n    >>> jnp.where(condlist[0],\n    ...   choicelist[0],\n    ...   jnp.where(condlist[1],\n    ...     choicelist[1],\n    ...     jnp.where(condlist[2],\n    ...       choicelist[2],\n    ...       default)))\n    Array([ 10,   2, 300,   0], dtype=int32)\n\n    However, for efficiency it is implemented in terms of :func:`jax.lax.select_n`.\n  \"\"\"\n  if len(condlist) != len(choicelist):\n    msg = \"condlist must have length equal to choicelist ({} vs {})\"\n    raise ValueError(msg.format(len(condlist), len(choicelist)))\n  if len(condlist) == 0:\n    raise ValueError(\"condlist must be non-empty\")\n\n  util.check_arraylike(\"select\", *condlist, *choicelist, default)\n  condlist = [asarray(cond) for cond in condlist]\n  choicelist = [asarray(choice) for choice in choicelist]\n  default = asarray(default)\n\n  # Put the default at front with condition False because\n  # argmax returns zero for an array of False values.\n  choicelist = util.promote_dtypes(default, *choicelist)\n  conditions = stack(broadcast_arrays(False, *condlist))\n  idx = argmax(conditions.astype(bool), axis=0)\n  return lax.select_n(*broadcast_arrays(idx, *choicelist))\n\ndef is_replicated_or_unreduced(sharding: NamedSharding) -> bool:\n  if sharding.spec.partitions:\n    return False","sourceCodeStart":2846,"sourceCodeEnd":2882,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L2846-L2882","documentation":"jnp.select requires condlist and choicelist to have identical lengths because each condition maps to one choice. A length mismatch means JAX cannot pair conditions with results.","triggerScenarios":"Calling jnp.select([c1, c2], [a]) or jnp.select(conds, choices) where the lists were built independently and diverged in length.","commonSituations":"Building condition and choice lists in separate loops or from dicts with different keys; adding a new condition but forgetting the corresponding branch; version changes adding an extra case.","solutions":["Ensure len(condlist) == len(choicelist)","Build both lists together in one loop or zip sources so they stay in sync","Add an assert before calling jnp.select in tests"],"exampleFix":"// before\njnp.select([x>0, x<0], [1])\n// after\njnp.select([x>0, x<0], [1, -1])","handlingStrategy":"validation","validationCode":"assert len(condlist) == len(choicelist), 'condlist/choicelist length mismatch'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build condlist and choicelist by appending pairs in one loop","Zip sources so adding a case updates both lists"],"tags":["jax","select","length-mismatch"],"backgroundTag":"list-length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}