Sass list.set-nth() Function

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

What You’ll Learn

list.set-nth() belongs to the built-in sass:list module. It returns a copy of a list with one slot replaced. This page covers 1-based indexes, negatives from the end, immutability, pairing with list.nth, Dart Sass support, and five compiled examples.

01

Concept

Replace by index

02

Module

@use "sass:list"

03

Indexes

1-based

04

Negative

From the end

05

Immutable

Returns a copy

06

Practice

5 examples

What Is list.set-nth()?

When you need to swap one token in a spacing scale, change a border style, or update the last font in a stack, list.set-nth builds a new list with that slot changed—without rewriting the whole list by hand.

  • 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
💡
Beginner tip

Like list.nth, indexes start at 1, and -1 means the last item. Unlike list.nth, you get a new list back—reassign if you want to keep it.

📝 Syntax

Load the list module, then call the function:

styles.scss
@use "sass:list";

list.set-nth($list, $n, $value)

Parameters

ParameterTypeRequiredDescription
$listList (or map / single value)YesThe list to copy and update.
$nNumber (integer)Yes1-based index of the item to replace. Negative values count from the end. Must point at an existing item or Sass errors.
$valueAnyYesThe new value for that slot.

Return value

TypeResult
ListA new list with index $n replaced by $value
ErrorIf there is no existing element at index $n

📦 Loading sass:list

styles.scss
// Recommended — namespaced
@use "sass:list";
$next: list.set-nth(10px 20px 30px, 1, 2em);

// Optional — bring members into scope
@use "sass:list" as *;
$next: set-nth(10px 20px 30px, 1, 2em);

// Optional — custom namespace
@use "sass:list" as l;
$next: l.set-nth(10px 20px 30px, 1, 2em);
⚠️
Put @use first

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

🔍 Immutable Copies

Sass lists are not mutated in place. Always capture the return value—or the original list stays unchanged.

styles.scss
@use "sass:list";

$orig: 10px 20px 30px;

// Wrong expectation: $orig is still 10px 20px 30px
list.set-nth($orig, 2, 99px);

// Correct: keep the new list
$copy: list.set-nth($orig, 2, 99px);
// or update the same name:
// $orig: list.set-nth($orig, 2, 99px);

📋 list.set-nth vs list.nth

list.set-nthlist.nth
QuestionReplace the value at index nRead the value at index n
OutputA new listA single value
Original listUnchangedUnchanged
Typical pairBuild a variant token listInspect before or after

🛠 Compatibility

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

Implementation@use "sass:list"Global set-nth()
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";
Replace firstlist.set-nth(10px 20px 30px, 1, 2em)
Replace lastlist.set-nth($list, -1, $val)
Keep the result$list: list.set-nth($list, $n, $val);
Read a slotlist.nth($list, $n)
Legacy globalset-nth($list, $n, $value)

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 positive and negative replacements.

Example 1 — Replace First, Last, and a Font Slot (Official Docs)

Space lists and comma font stacks both work.

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

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

.demo {
  --a: #{meta.inspect(list.set-nth(10px 20px 30px, 1, 2em))};
  --b: #{meta.inspect(list.set-nth(10px 20px 30px, -1, 8em))};
  --c: #{meta.inspect(list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto))};
}

How It Works

Index 1 updates the first item; -1 updates the last. Separator style (space vs comma) is preserved on the returned list.

Example 2 — Tweak Border Width or Style

Start from one border token list and derive variants.

styles.scss
@use "sass:list";

$border: 1px solid #336699;
$border-dashed: list.set-nth($border, 2, dashed);
$border-wide: list.set-nth($border, 1, 3px);

.box {
  border: $border-dashed;
}

.wide {
  border: $border-wide;
}

How It Works

Slot 2 is the style keyword; slot 1 is the width. Each call returns a full border shorthand ready for CSS.

📈 Practical Patterns

Scales, fonts, and proving immutability.

Example 3 — Adjust First and Last Spacing Tokens

Derive a denser start or a larger end from one scale.

styles.scss
@use "sass:list";

$gaps: 0.25rem 0.5rem 1rem 1.5rem 2rem;
$gaps-lg: list.set-nth($gaps, -1, 3rem);
$gaps-sm: list.set-nth($gaps, 1, 0.125rem);

.scale {
  --orig-last: #{list.nth($gaps, -1)};
  --new-last: #{list.nth($gaps-lg, -1)};
  --new-first: #{list.nth($gaps-sm, 1)};
  padding: $gaps-lg;
}

How It Works

The original $gaps still ends in 2rem. The derived lists carry the replacements, and $gaps-lg becomes a five-value padding shorthand.

Example 4 — Swap the Final Font Family

Replace the generic fallback (or any slot) in a comma stack.

styles.scss
@use "sass:list";

$fonts: Helvetica, Arial, sans-serif;
$fonts-roboto: list.set-nth($fonts, 3, Roboto);

.card {
  font-family: $fonts-roboto;
}

How It Works

Index 3 was sans-serif; after set-nth it is Roboto, still comma-separated for font-family.

Example 5 — Prove the Original List Is Unchanged

Compare the source list with the returned copy side by side.

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

$orig: 10px 20px 30px;
$copy: list.set-nth($orig, 2, 99px);

.compare {
  --orig: #{meta.inspect($orig)};
  --copy: #{meta.inspect($copy)};
}

How It Works

$orig is untouched. Only $copy has the middle value replaced—the key mental model for every Sass list helper that “updates” a list.

🚀 Real-World Use Cases

  • Theme variants — tweak one slot of a shared spacing or size scale.
  • Border / shorthand tokens — swap width, style, or color independently.
  • Font stacks — replace a family without rewriting the whole comma list.
  • Mixin APIs — accept a base list and return a customized copy.
  • Safe updates — combine with list.index when the slot is found by value.

🧠 How Compilation Works

1

Write SCSS

Call list.set-nth($list, $n, $value) after @use "sass:list".

Source
2

Copy and replace

Dart Sass builds a new list and writes $value into the resolved index.

Compile
3

Return the copy

You receive a new list; the original variable is unchanged until you reassign.

Result
4

CSS sees the list

Properties like border or font-family get the finished values.

⚠️ Common Pitfalls

  • Forgetting to reassign — calling set-nth alone does not update the variable.
  • 0-based thinking — the first item is 1, not 0.
  • Out-of-range indexes — Sass errors if the slot does not exist.
  • Confusing with nthnth reads; set-nth returns a modified copy.
  • Forgetting @uselist.set-nth needs sass:list.

💡 Best Practices

✅ Do

  • Use @use "sass:list" and list.set-nth() in new SCSS
  • Reassign: $list: list.set-nth($list, $n, $val)
  • Use -1 when replacing the last item
  • Verify slots with list.nth while debugging
  • Prefer Dart Sass for built-in modules

❌ Don’t

  • Expect the browser to evaluate list.set-nth
  • Assume the original list variable updates in place
  • Use index 0 for the first item
  • Call set-nth past list.length
  • Rely on LibSass for @use "sass:list"

Key Takeaways

Knowledge Unlocked

Five things to remember about list.set-nth()

Replace by 1-based index—get a new list; reassign to keep it.

5
Core concepts
📦 02

Module

sass:list

@use
🔢 03

Indexes

start at 1

Rule
04

Returns

a new list

Safe
05

Prefer

Dart Sass 1.23+

Tooling

❓ 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.
Did you know?

To add an item instead of replacing one, use list.append() or list.join(). set-nth only works on indexes that already exist.

Conclusion

list.set-nth() returns a new list with one 1-based index replaced. Load it through sass:list, use negatives for end slots, reassign to keep the result, and stay inside list.length to avoid compile errors.

Continue with list.slash() or explore list.nth().

Next: list.slash()

You learned list.set-nth()—next, build slash-separated lists with list.slash().

list.slash() →

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