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.
Practice
length() do when used on a list?Solution
Step 1: Understand the purpose of
Thelength()length()function counts how many items are inside a list.Step 2: Compare with other functions
Functions likenth()get an item,append()adds items, andjoin()combines lists, so they do different things.Final Answer:
Returns the number of items in the list -> Option AQuick Check:
length()= count items [OK]
- Confusing length() with nth()
- Thinking length() adds items
- Mixing length() with join()
$colors in Sass?Solution
Step 1: Recall the function to access list items
In Sass,nth()is used to get the item at a specific position in a list.Step 2: Check syntax correctness
The syntax isnth($list, index). Other options likeget()or bracket notation are invalid in Sass.Final Answer:
nth($colors, 3) -> Option CQuick Check:
nth() = get item by position [OK]
- Using bracket notation like arrays
- Using non-existent functions like get()
- Mixing up argument order
$list1: 10px 20px 30px; $list2: 40px 50px; $result: join($list1, $list2);
What is the value of $result?
Solution
Step 1: Understand join() function behavior
Thejoin()function combines two lists into one list separated by a comma by default.Step 2: Apply join() to given lists
Joining$list1and$list2results in a list:10px 20px 30px, 40px 50px(comma-separated).Final Answer:
10px 20px 30px, 40px 50px -> Option AQuick Check:
join() merges lists with comma separator by default [OK]
- Thinking join() concatenates without separator
- Reversing list order
- Assuming join() needs three arguments
$my-list: 1 2 3; $new-list: append($my-list, 4, comma);
Solution
Step 1: Check append() function syntax
The third argument forappend()must be a string: either'space'or'comma'.Step 2: Identify the error in the code
The code usescommawithout quotes, which is invalid syntax in Sass.Final Answer:
The append() function requires the separator to be 'space' or 'comma' as a string, not a bare word -> Option BQuick Check:
Separator must be a quoted string [OK]
- Forgetting quotes around separator
- Thinking append() only takes two arguments
- Assuming lists need parentheses
$listA: red green blue; $listB: yellow orange;
You want to create a new list that contains all items from $listA followed by $listB, separated by spaces, and then add black at the end. Which Sass code correctly does this?
Solution
Step 1: Join $listA and $listB with space separator
Usejoin($listA, $listB, 'space')to combine both lists with spaces.Step 2: Append 'black' to the joined list with space separator
Useappend(..., black, 'space')to add 'black' at the end, keeping space separation.Step 3: Verify other options
$combined: join(append($listA, black, 'space'), $listB, 'comma');uses comma separator incorrectly;$combined: append($listA, join($listB, black, 'space'), 'space');nests join inside append wrongly;$combined: join($listA, $listB, 'space') append(black);syntax is invalid.Final Answer:
$combined: append(join($listA, $listB, 'space'), black, 'space'); -> Option DQuick Check:
join() then append() with 'space' separator [OK]
- Mixing comma and space separators
- Appending before joining lists
- Incorrect function chaining syntax
Ready to prove it?
Pass the test and this pattern moves unknown
