TheAlgorithms/Python · error · ValueError
Set a and b must either both be sets or be either a list or
Error message
Set a and b must either both be sets or be either a list or a tuple.
What it means
Raised by jaccard_similarity in maths/jaccard_similarity.py when set_a and set_b are not a supported combination. The function handles exactly two cases: both arguments are sets, or both are lists/tuples. Any mixed pairing (set with list) or other types (str, dict, int) falls through to the final raise ValueError at the end of the function. The message summarizes the accepted type combinations.
Source
Thrown at maths/jaccard_similarity.py:87
intersection_length = len(set_a.intersection(set_b))
if alternative_union:
union_length = len(set_a) + len(set_b)
else:
union_length = len(set_a.union(set_b))
return intersection_length / union_length
elif isinstance(set_a, (list, tuple)) and isinstance(set_b, (list, tuple)):
intersection = [element for element in set_a if element in set_b]
if alternative_union:
return len(intersection) / (len(set_a) + len(set_b))
else:
# Cast set_a to list because tuples cannot be mutated
union = list(set_a) + [element for element in set_b if element not in set_a]
return len(intersection) / len(union)
raise ValueError(
"Set a and b must either both be sets or be either a list or a tuple."
)
if __name__ == "__main__":
set_a = {"a", "b", "c", "d", "e"}
set_b = {"c", "d", "e", "f", "h", "i"}
print(jaccard_similarity(set_a, set_b))
View on GitHub (pinned to f5988cc097)
Solutions
- Normalize both sides to the same type before calling: jaccard_similarity(set(a), set(b)).
- Wrap strings in lists for token/char comparison: jaccard_similarity(list(s1), list(s2)).
- Convert dict comparisons to their key or item views: jaccard_similarity(set(d1), set(d2)).
Example fix
// before score = jaccard_similarity(a, b) # a is a set, b is a list // after score = jaccard_similarity(set(a), set(b))
Defensive patterns
Strategy: type-guard
Validate before calling
a, b = set(a), set(b) # normalize both sides score = jaccard_similarity(a, b)
Type guard
def is_supported_jaccard_pair(a, b) -> bool:
both_sets = isinstance(a, set) and isinstance(b, set)
both_seqs = isinstance(a, (list, tuple)) and isinstance(b, (list, tuple))
return both_sets or both_seqs Try / catch
try:
s = jaccard_similarity(a, b)
except ValueError:
s = jaccard_similarity(set(a), set(b)) Prevention
- Normalize both collections to the same type before comparing
- Wrap strings in list() for character-level similarity
When it happens
Trigger: Calling jaccard_similarity({1,2}, [1,2]) (mixed set and list), jaccard_similarity('abc', 'abd') (strings), or jaccard_similarity({'a':1}, {'b':2}) (dicts). Both arguments must be the same supported kind.
Common situations: Datasets where one side came from set() dedup and the other from a JSON list; passing strings expecting character-level comparison; API glue code where the two collections have different provenance.
Related errors
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
- surface_area_cone() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/86a70d5382af9f03.
Report an issue: GitHub.