Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Module

.list.len

Counts the number of elements in every list represented by an expression. Null elements are included, and the result is a UInt32 expression.

How it works

Apply .list.len() to a list-valued expression, such as pl.col("a"), to produce an expression that counts the items in every row's list. The result has UInt32 type, and an item whose value is null still contributes to the count.

Syntax

Expr.list.len() → Expr

Returns

Expr of data type UInt32

Key points

  • The method computes one list-element count for each row.
  • Null entries are counted as elements.
  • The method is available on list expressions and returns another expression.
  • The resulting expression uses UInt32 values.
  • A list column can be measured directly in with_columns.

Common mistakes

  • Null elements are included in the reported list length; they are not skipped.

Example: Count items in list columns

import polars as pl

df = pl.DataFrame({"a": [[1, 2, None], [5], []]})
result = df.with_columns(length=pl.col("a").list.len())
print(result)

Adds a UInt32 column containing the number of items in each list, including null items.

When to use it

List-size feature engineering

Add a derived column containing the size of each row's list-valued field.

List cardinality checks

Filter or inspect rows based on whether their nested lists are empty or have a target size.

Nested-data profiling

Summarize nested data before exploding or otherwise reshaping list columns.

Interview questions

QWhat does Expr.list.len() return?

Expr.list.len() returns an expression that counts the number of elements in each list. Its result has UInt32 data type, and null elements are included in that count.

Follow-up: How would you apply it to a DataFrame column? Use df.with_columns(pl.col("a").list.len()) or give the resulting expression an output name.

QHow do you use this method in a DataFrame transformation?

Use a list-valued column expression, such as pl.col("a").list.len(), inside a DataFrame expression context. Each output value is the length of the corresponding row's list.

Follow-up: How does an empty list behave? It contributes zero elements, so its resulting length is 0.

QWhat happens when a list contains null values?

The operation counts list positions rather than only non-null values. Therefore, a list containing two integers and one null produces a length of three.

Follow-up: How would you count only non-null items? First transform or filter the list values to remove nulls, then apply a length operation.

  • Expr.list
  • Expr.list.__getitem__
  • Expr.list.all
  • Expr.list.eval

Source: polars.Expr.list.len — Polars documentation. Checked 2026-10-15.