list.nth($list, $n) returns the element of $list at index $n. Indexes are 1-based. If $n is negative, it counts from the end (-1 is last). Load it with @use "sass:list" on Dart Sass 1.23+. Global alias: nth().
Out-of-range indexes error.
Official docs: Sass throws if there is no element at $n. Guard with list.length(), or only call nth after list.index() returns a non-null index.
Guard out-of-range with length or a non-null index.
Prefer list.nth after @use "sass:list".
One line: when you need the value at a position, call list.nth($list, $n).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:list"
2. Remember indexes are 1-based
3. Use -1 for the last item
4. Never pass a null index into nth
5. Loop with @for $i from 1 through list.length($list)
Frequently Asked Questions
list.nth($list, $n) returns the item at index $n. Example: list.nth(10px 12px 16px, 2) is 12px. Indexes start at 1.
Official docs: negative $n counts from the end. list.nth([line1, line2, line3], -1) is line3. -2 is the second-to-last item.
Sass throws an error. Check against list.length, or only call nth after list.index returns a non-null index.
list.index finds a value’s position. list.nth returns the value at a position. They are often used together.
Yes. Global nth($list, $n) still works. New projects should prefer list.nth() 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 nth() name there if you must.