Built-in list functions help you work with groups of values easily. They save time and make your code cleaner.
Built-in list functions in SASS
@function list-function-name($list, $optional-arguments) {
// Use built-in list functions like length(), nth(), append(), join(), index()
}List functions work with lists written like: (red, green, blue) or red, green, blue.
Use parentheses for clarity, but Sass also understands space or comma separated lists.
$colors, which is 3.$colors: (red, green, blue); $length: length($colors);
$sizes, which is 10px.$sizes: 10px 20px 30px; $first-size: nth($sizes, 1);
Verdana to the end of the $fonts list.$fonts: (Arial, Helvetica); $new-fonts: append($fonts, Verdana);
$list1: (1, 2); $list2: (3, 4); $joined: join($list1, $list2);
This Sass code shows how to use built-in list functions to get the length, get an item, add an item, and join lists. The @debug lines print the results in the console.
@use 'sass:list'; $colors: (red, green, blue); // Get length of list $color-count: list.length($colors); // Get first color $first-color: list.nth($colors, 1); // Add a new color $updated-colors: list.append($colors, purple); // Join two lists $more-colors: (yellow, orange); $all-colors: list.join($updated-colors, $more-colors); // Output results @debug 'Number of colors: #{$color-count}'; @debug 'First color: #{$first-color}'; @debug 'Colors after append: #{inspect($updated-colors)}'; @debug 'All colors joined: #{inspect($all-colors)}';
Time complexity: List functions run quickly because lists are simple arrays.
Space complexity: Functions like append() and join() create new lists, so they use extra memory.
Common mistake: Forgetting that list indexes start at 1, not 0.
Use append() to add items, but use join() to combine two lists.
Built-in list functions make working with groups of values easy and clean.
Remember list indexes start at 1 in Sass.
Use functions like length(), nth(), append(), and join() to manage lists.