TheAlgorithms/Python · error · ValueError

The parameter days should be a list of integers

Error message

The parameter days should be a list of integers

What it means

Raised as ValueError by mincost_tickets(days, costs) when days is not a list or any element is not an int (isinstance(days, list) and all(isinstance(day, int) ...)). The function models the LeetCode 'minimum cost for tickets' problem over days 1..365, so days must be a list of integers. It is the first of four validation checks, followed by costs shape, empty days (returns 0), and the 1..365 range checks.

Source

Thrown at dynamic_programming/minimum_tickets_cost.py:93

    >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [])
    Traceback (most recent call last):
     ...
    ValueError: The parameter costs should be a list of three integers

    >>> mincost_tickets([], [])
    Traceback (most recent call last):
     ...
    ValueError: The parameter costs should be a list of three integers

    >>> mincost_tickets([2, 3, 4, 5, 6, 7, 8, 9, 10, 30, 31], [1, 2, 3, 4])
    Traceback (most recent call last):
     ...
    ValueError: The parameter costs should be a list of three integers
    """

    # Validation
    if not isinstance(days, list) or not all(isinstance(day, int) for day in days):
        raise ValueError("The parameter days should be a list of integers")

    if len(costs) != 3 or not all(isinstance(cost, int) for cost in costs):
        raise ValueError("The parameter costs should be a list of three integers")

    if len(days) == 0:
        return 0

    if min(days) <= 0:
        raise ValueError("All days elements should be greater than 0")

    if max(days) >= 366:
        raise ValueError("All days elements should be less than 366")

    days_set = set(days)

    @functools.cache
    def dynamic_programming(index: int) -> int:
        if index > 365:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Normalize before calling: mincost_tickets([int(d) for d in days], costs).
  2. Convert tuples/arrays to list: list(days) / days.tolist().
  3. Validate at ingestion that every day is an int within 1..365, since later checks enforce that range anyway.

Example fix

# before
cost = mincost_tickets(days, [2, 7, 15])  # days = ['1', '4', '5'] -> ValueError

# after
cost = mincost_tickets([int(d) for d in days], [2, 7, 15])
Defensive patterns

Strategy: type-guard

Validate before calling

days = [int(d) for d in days] if not isinstance(days, list) or not all(isinstance(d, int) for d in days) else days
cost = mincost_tickets(days, costs)

Type guard

def is_int_day_list(days: object) -> bool:
    return isinstance(days, list) and all(
        isinstance(d, int) and not isinstance(d, bool) for d in days
    )

Try / catch

try:
    cost = mincost_tickets(days, costs)
except ValueError as exc:
    if 'days should be a list' in str(exc):
        cost = mincost_tickets([int(d) for d in days], costs)
    else:
        raise

Prevention

When it happens

Trigger: mincost_tickets((1,2,3), [1,2,3]) with a tuple instead of a list; mincost_tickets(['1','2'], [1,2,3]) with string days; mincost_tickets([1.0, 2.0], [1,2,3]) with floats; numpy arrays also fail the isinstance(days, list) check.

Common situations: Travel dates parsed from CSV/JSON as strings or floats; tuples from config or function returns; numpy date ordinals from pandas pipelines.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/a2423df6c872da7d. Report an issue: GitHub.