Sass list.length() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:list · Dart Sass 1.23+

What You’ll Learn

list.length() belongs to the built-in sass:list module. It counts how many items a list holds. This page covers single values, empty lists, maps as lists of pairs, looping with @for and list.nth, Dart Sass support, and five compiled examples.

01

Concept

Count items

02

Module

@use "sass:list"

03

Singles

Length 1

04

Maps

Count pairs

05

Pair with

@for + nth

06

Practice

5 examples

What Is list.length()?

Before you loop a spacing scale or check whether a font stack has enough fallbacks, you need a count. list.length returns that number at compile time.

  • list.length(10px)1
  • list.length(10px 20px 30px)3
  • list.length((width: 10px, height: 20px))2
💡
Beginner tip

In Sass, every value is also a list. That is why a lone 10px has length 1—not “not a list.” Maps count as lists of key/value pairs.

📝 Syntax

Load the list module, then call the function:

styles.scss
@use "sass:list";

list.length($list)

Parameters

ParameterTypeRequiredDescription
$listList (or map / single value)YesThe list to measure. Maps and single values also count as lists in Sass.

Return value

TypeResult
NumberHow many items are in $list (or how many pairs, for a map)

📦 Loading sass:list

styles.scss
// Recommended — namespaced
@use "sass:list";
$n: list.length(10px 20px 30px);

// Optional — bring members into scope
@use "sass:list" as *;
$n: length(10px 20px 30px);

// Optional — custom namespace
@use "sass:list" as l;
$n: l.length(10px 20px 30px);
⚠️
Put @use first

Keep @use "sass:list"; near the top of the file, before most other rules.

🔍 Length of Maps

Official docs note that every map counts as a list of two-element pairs. So list.length on a map returns the number of entries—handy before you loop keys or validate a theme object.

styles.scss
@use "sass:list";
@use "sass:map";

$theme: (
  primary: #336699,
  accent: #e67e22,
  muted: #94a3b8,
);

$pairs: list.length($theme);      // 3
$keys: list.length(map.keys($theme)); // also 3

📋 list.length vs list.nth

list.lengthlist.nth
QuestionHow many items?What is at index n?
OutputNumberA value (or error if out of range)
Typical pair@for $i from 1 through list.length($list)list.nth($list, $i) inside the loop

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS only contains the finished numbers or property values you choose to emit.

Implementation@use "sass:list"Global length()
Dart Sass (1.23+)Yes — preferredYes
LibSassNo built-in modulesYes (legacy)
Ruby SassNo built-in modulesYes (legacy)

⚡ Quick Reference

GoalCode
Import list@use "sass:list";
Count a listlist.length(10px 20px 30px)3
Single valuelist.length(10px)1
Empty listlist.length(())0
Map pairslist.length((a: 1, b: 2))2
Safe loop@for $i from 1 through list.length($list) { … }
Legacy globallength($list)

Examples Gallery

Each example uses @use "sass:list". Open View Compiled CSS for verified Dart Sass output (official samples where noted).

📚 Getting Started

Official docs samples for singles, lists, and maps.

Example 1 — Count Singles, Lists, and Maps (Official Docs)

Three classic shapes from the Sass documentation.

styles.scss
@use "sass:list";

@debug list.length(10px); // 1
@debug list.length(10px 20px 30px); // 3
@debug list.length((width: 10px, height: 20px)); // 2

.count {
  --a: #{list.length(10px)};
  --b: #{list.length(10px 20px 30px)};
  --c: #{list.length((width: 10px, height: 20px))};
}

How It Works

A lone value is a one-item list. A three-value space list is length 3. A two-entry map reports 2 pairs.

Example 2 — Scales, Empty Lists, and Brackets

Length ignores bracket style—only the item count matters.

styles.scss
@use "sass:list";

$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;
$empty: ();
$brack: [a, b, c];

.shapes {
  --gaps: #{list.length($gaps)};
  --empty: #{list.length($empty)};
  --brack: #{list.length($brack)};
}

How It Works

Empty () is length 0. Bracketed [a, b, c] is still three items. Use list.is-bracketed() if you need the bracket flag.

📈 Practical Patterns

Theme maps, generated utility classes, and font stacks.

Example 3 — Count Theme Map Entries

Validate how many tokens a theme map holds.

styles.scss
@use "sass:list";
@use "sass:map";

$theme: (
  primary: #336699,
  accent: #e67e22,
  muted: #94a3b8,
);

.theme {
  --map-len: #{list.length($theme)};
  --map-keys: #{list.length(map.keys($theme))};
}

How It Works

Counting the map directly matches counting map.keys($theme) for a normal map—both report three entries here.

Example 4 — Loop with @for and list.nth

Generate utility classes from a spacing scale.

styles.scss
@use "sass:list";

$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;

@for $i from 1 through list.length($gaps) {
  .gap-#{$i} {
    margin-top: list.nth($gaps, $i);
  }
}

How It Works

Indexes are 1-based, matching list.nth. Length sets the loop upper bound so you never walk past the end of the list.

Example 5 — Inspect a Font Stack

Expose how many families sit in a comma-separated stack.

styles.scss
@use "sass:list";

$fonts: Helvetica, Arial, sans-serif;

.card {
  --font-count: #{list.length($fonts)};
  font-family: $fonts;
}

How It Works

Comma separators do not change counting—three families means length 3, while the stack still compiles as normal CSS.

🚀 Real-World Use Cases

  • Utility generators — drive @for loops over spacing or size scales.
  • Theme validation — assert a map has the expected number of tokens.
  • Mixin guards — skip logic when a list is empty (length == 0).
  • Font / shadow stacks — document or debug how many items you emit.
  • Safe indexing — compare an index against length before calling list.nth.

🧠 How Compilation Works

1

Write SCSS

Call list.length($list) after @use "sass:list".

Source
2

Count items

Dart Sass tallies list items (or map pairs) at compile time.

Compile
3

Return a number

Use it in custom properties, @if guards, or @for bounds.

Result
4

CSS sees numbers

No Sass call remains—only the finished values you emitted.

⚠️ Common Pitfalls

  • 0-based loops — Sass list indexes start at 1, so use from 1 through length.
  • Nested lists — a nested sublist counts as one item unless you flatten first.
  • Expecting CSS length() — this is Sass compile-time, not the CSS length() idea.
  • Forgetting @uselist.length needs sass:list.
  • Confusing maps with lists of values — map length is pair count, not flattened values.

💡 Best Practices

✅ Do

  • Use @use "sass:list" and list.length() in new SCSS
  • Drive @for with 1 through list.length($list)
  • Treat singles as length 1 when writing helpers
  • Use length to guard empty lists before indexing
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to evaluate list.length
  • Start list loops at 0
  • Assume brackets change the count
  • Rely on LibSass for @use "sass:list"
  • Call list.nth past list.length without a check

Key Takeaways

Knowledge Unlocked

Five things to remember about list.length()

Count items (or map pairs)—then loop safely with 1-based indexes.

5
Core concepts
📦 02

Module

sass:list

@use
🔢 03

Singles

length 1

Rule
04

Maps

count pairs

Safe
05

Prefer

Dart Sass 1.23+

Tooling

❓ Frequently Asked Questions

list.length($list) returns how many items are in $list. Example: list.length(10px 20px 30px) is 3. A single value like 10px has length 1.
Yes. Official docs: it can return the number of pairs in a map. list.length((width: 10px, height: 20px)) is 2.
0. For example list.length(()) is 0.
No. Bracketedness is separate from length. list.length([a, b, c]) is still 3.
Yes. Global length($list) still works. New projects should prefer list.length() after @use "sass:list".
Built-in modules with @use need Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:list the same way—use the global length() name there if you must.
Did you know?

Because maps are lists of pairs, advanced list helpers like list.nth can also work on maps. Still, for map-specific work prefer sass:map helpers such as map.keys and map.values.

Conclusion

list.length() counts items in a Sass list (or pairs in a map). Load it through sass:list, remember that singles have length 1, and pair it with @for plus list.nth for safe loops.

Continue with list.separator() or explore list.join().

Next: list.separator()

You learned list.length()—next, detect separators with list.separator().

list.separator() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful