pandas-dev/pandas · error · TypeError

' ' object is not iterable

Error message

'{type(self).__name__}' object is not iterable

What it means

`TypeError("'{type}' object is not iterable")` from `ListAccessor.__iter__`. The accessor deliberately disables iteration: iterating the accessor object itself is meaningless (you want to iterate the Series, or use `.flatten()`/`.list[i]`). Implementing `__getitem__` would otherwise make Python silently call `__iter__` in `for` loops and produce confusing results, so pandas raises explicitly.

Solutions

  1. Iterate the Series instead: `for row in s:` gives each row (a Python list).
  2. To work flat, use `s.list.flatten()` and iterate the result.
  3. To get a Python list of the lists: `s.to_numpy().tolist()`.

Example fix

// before
for sub in s.list:   # TypeError
    ...

// after
for sub in s:        # iterate the Series
    ...
# or
flat = s.list.flatten()
Defensive patterns

Strategy: type-guard

Validate before calling

# do not iterate the accessor; iterate the Series
for row in s:
    process(row)

Type guard

def is_iterable_target(obj) -> bool:
    # the Series is iterable; the ListAccessor is not
    import pandas as pd
    return isinstance(obj, (pd.Series, list, tuple))

Try / catch

try:
    for sub in s.list:
        ...
except TypeError as e:
    if 'not iterable' in str(e):
        for sub in s:
            ...
    else:
        raise

Prevention

When it happens

Trigger: `for x in s.list:`; `list(s.list)`; `*rest, = s.list`; any context that triggers Python's iteration protocol on the `ListAccessor` object.

Common situations: Assuming `s.list` yields the per-row lists; confusing the accessor with the underlying Series; refactoring from `for row in s:` to `for row in s.list:`.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/e91b2ea7298862ac. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/arrow/accessors.py:198

            if start is None:
                # TODO: When adding negative step support
                #  this should be set to last element of array
                # when step is negative.
                start = 0
            if step is None:
                step = 1
            sliced = pc.list_slice(self._pa_array, start, stop, step)
            return Series(
                sliced,
                dtype=ArrowDtype(sliced.type),
                index=self._data.index,
                name=self._data.name,
            )
        else:
            raise ValueError(f"key must be an int or slice, got {type(key).__name__}")

    def __iter__(self) -> Iterator:
        raise TypeError(f"'{type(self).__name__}' object is not iterable")

    def flatten(self) -> Series:
        """
        Flatten list values.

        Each list element is expanded into separate rows, preserving the
        original index. The resulting Series may have a longer length than
        the original if lists contain more than one element.

        Returns
        -------
        pandas.Series
            The data from all lists in the series flattened.

        See Also
        --------
        ListAccessor.__getitem__ : Index or slice values in the Series.

View on GitHub (pinned to 3b7651241d)