Sass list.set-nth()

Beginner
12 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

list.set-nth($list, $n, $value) returns a copy of $list with the element at index $n replaced by $value. Indexes are 1-based. Negative $n counts from the end (-1 is last). Load it with @use "sass:list" on Dart Sass 1.23+. Global alias: set-nth().

Out-of-range indexes error.

Official docs: Sass throws if there is no existing element at $n. Guard with list.length(), or only set after list.index() returns a non-null index.

Official samples.

list.set-nth(10px 20px 30px, 1, 2em) → 2em 20px 30px. list.set-nth(10px 20px 30px, -1, 8em) → 10px 20px 8em. list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto) → Helvetica, Arial, Roboto.

Syntax

SCSS
@use "sass:list";

list.set-nth(10px 20px 30px, 1, 2em);  // 2em 20px 30px
list.set-nth(10px 20px 30px, -1, 8em); // 10px 20px 8em
list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto);
// Helvetica, Arial, Roboto

// Global alias still works
set-nth(10px 20px 30px, 2, 1rem);

Pattern: find a slot with index or pick a fixed position → rewrite with set-nth → reassign if you need to keep growing the same variable.

Index Rules

How replacement works:

IdeaMeaning
$n: 1Replace the first item
$n: -1Replace the last item
ImmutabilityReturns a new list; original is unchanged
vs list.nthnth reads; set-nth writes a copy
Out of range / nullCompile error — guard first
Separator is preserved.

Space lists stay space-separated; comma lists stay comma-separated after the replacement.

SCSS Examples

Examples target Dart Sass 1.23+ with @use "sass:list". Open View Compiled CSS for verified output (matches official Sass docs samples).

Example 1 — Replace the First Item (Official Docs)

Swap the leading spacing token for 2em.

SCSS
@use "sass:list";

@debug list.set-nth(10px 20px 30px, 1, 2em); // 2em 20px 30px

.box {
  margin: list.set-nth(10px 20px 30px, 1, 2em);
}

How It Works

  1. Index 1 targets the first item.
  2. 10px becomes 2em; the rest stay.
  3. Space separator is preserved.
  4. Matches the official Sass docs sample.

Example 2 — Replace the Last Item with -1 (Official Docs)

Negative indexes count from the end.

SCSS
@use "sass:list";

@debug list.set-nth(10px 20px 30px, -1, 8em); // 10px 20px 8em

.last {
  --sizes: #{list.set-nth(10px 20px 30px, -1, 8em)};
}

How It Works

  1. -1 means the last item.
  2. 30px becomes 8em.
  3. Earlier items are unchanged.
  4. Handy for “override the final step” on a scale.

Example 3 — Rewrite a Font Stack Fallback (Official Docs)

Replace the third comma-separated family.

SCSS
@use "sass:list";

@debug list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto);
// Helvetica, Arial, Roboto

.fonts {
  font-family: list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto);
}

How It Works

  1. Comma list of three font names.
  2. Index 3 replaces sans-serif with Roboto.
  3. Comma separator stays intact.
  4. Matches the official Sass docs sample.

Example 4 — Find with index, Then Replace

Change border style without hard-coding the slot.

SCSS
@use "sass:list";

$border: 1px solid red;
$i: list.index($border, solid);

.border {
  @if $i != null {
    border: list.set-nth($border, $i, dashed);
  }
}

How It Works

  1. index finds the style slot.
  2. @if $i != null blocks a bad set-nth call.
  3. Replacement writes dashed in place of solid.
  4. Width and color stay the same.

Example 5 — Reassign to Apply Multiple Edits

Each call returns a new list — reassign to keep changes.

SCSS
@use "sass:list";

$sizes: 8px 16px 24px 32px;
$sizes: list.set-nth($sizes, 2, 18px);
$sizes: list.set-nth($sizes, -1, 40px);

.chain {
  padding: $sizes;
}

How It Works

  1. First reassignment changes index 2 to 18px.
  2. Second changes the last item to 40px.
  3. Without reassign, earlier results would be discarded.
  4. Final padding reflects both edits.

Edge Cases & Pitfalls

Most set-nth bugs are forgetting to reassign, passing a null index, or writing past list.length.

immut

Reassign to keep the result

Write $list: list.set-nth($list, $n, $val); — the helper does not mutate in place.

range

Out of range throws

Compare against list.length before hard-coded indexes.

null

Never pass index null

Guard after list.index with @if $i != null.

base

Indexes start at 1

Same convention as list.nth — there is no index 0.

Sass Support

Prefer the module form with Dart Sass 1.23+. Global set-nth() remains for older call sites.

APIStatus
list.set-nth()Dart Sass 1.23+ via @use "sass:list"
Global set-nth()Available (prefer module form in new code)
Indexes1-based; negatives count from the end
LibSass / Ruby SassNo sass:list module — use global set-nth() if needed

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a new list with one index replaced.
  • Negative indexes replace from the end (-1 = last).
  • Reassign when applying multiple edits to one variable.
  • Prefer list.set-nth after @use "sass:list".

One line: when you need to replace the value at a position, call list.set-nth($list, $n, $value).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:list"
2. Remember indexes are 1-based
3. Use -1 to replace the last item
4. Reassign: $list: list.set-nth($list, $n, $val)
5. Guard null indexes from list.index before set-nth

Frequently Asked Questions

list.set-nth($list, $n, $value) returns a copy of $list with the item at index $n replaced by $value. Example: list.set-nth(10px 20px 30px, 1, 2em) is 2em 20px 30px.
No. It returns a new list. The original variable stays the same unless you reassign, for example $list: list.set-nth($list, 2, $val).
Official docs: negative $n counts from the end. list.set-nth(10px 20px 30px, -1, 8em) replaces the last item, yielding 10px 20px 8em.
Sass throws an error if there is no existing element at $n. Check list.length first, or only set after a valid list.index result.
list.nth reads the value at an index. list.set-nth returns a new list with that index replaced.
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 set-nth() name there if you must.

Next: Sass list.slash()

Build a slash-separated list for CSS values like font-size/line-height.

list.slash() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful