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
Concept
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
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.
Foundation
📝 Syntax
Load the list module, then call the function:
styles.scss
@use "sass:list";
list.set-nth($list, $n, $value)
Parameters
Parameter
Type
Required
Description
$list
List (or map / single value)
Yes
The list to copy and update.
$n
Number (integer)
Yes
1-based index of the item to replace. Negative values count from the end. Must point at an existing item or Sass errors.
$value
Any
Yes
The new value for that slot.
Return value
Type
Result
List
A new list with index $n replaced by $value
Error
If there is no existing element at index $n
Modules
📦 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.
Details
🔍 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);
Compare
📋 list.set-nth vs list.nth
list.set-nth
list.nth
Question
Replace the value at index n
Read the value at index n
Output
A new list
A single value
Original list
Unchanged
Unchanged
Typical pair
Build a variant token list
Inspect before or after
Support
🛠 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 — preferred
Yes
LibSass
No built-in modules
Yes (legacy)
Ruby Sass
No built-in modules
Yes (legacy)
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import list
@use "sass:list";
Replace first
list.set-nth(10px 20px 30px, 1, 2em)
Replace last
list.set-nth($list, -1, $val)
Keep the result
$list: list.set-nth($list, $n, $val);
Read a slot
list.nth($list, $n)
Legacy global
set-nth($list, $n, $value)
Hands-On
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))};
}
📤 Compiled CSS:
.demo {
--a: 2em 20px 30px;
--b: 10px 20px 8em;
--c: Helvetica, Arial, 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.
$orig is untouched. Only $copy has the middle value replaced—the key mental model for every Sass list helper that “updates” a list.
Applications
🚀 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.
Watch Out
⚠️ 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 nth — nth reads; set-nth returns a modified copy.
Forgetting @use — list.set-nth needs sass:list.
Pro Tips
💡 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"
Summary
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
📝01
Call
list.set-nth($list, $n, $value)
API
📦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.
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.