# Lists --- CS 65 // 2021-03-16 ## The `list` Type - **Purpose:** + To manipulate collections of objects - **Express:** + Lists are written: `[1,2,3]` and `["CS", 65]` - **Operations:** + `t[index]`, gets the object at the given `index` + `t[start:end]`, gets the **sublist** that starts at index `start` and ends at index `end` + `t1 + t2`, concatenates the two lists together ## The `list` Type - **Other Operations**: + `len(t)`, gets the **length** of the list `t` + `t.append(value)`: mutates `t` by adding `value` as a new element to the end of the list + `t.sort()`: mutates `t` so that all of its elements are in ascending order + `t.pop(index)`: mutates `t` by removing the element at the given `index` ## Mutability - `list` is a **mutable** type + It is possible to change the values a list contains **without creating a new list** + `lst = [100, 101, 102]` + `lst[0] = "abc"`, changes the first element - Other methods like `append`, `sort`, and `pop` also mutate the list rather than returning a new one # Stack Diagrams ## Strings and Lists - The string method `s.split()` converts a string into a list of words + It can be given an optional "delimiter" to split by other textual cues like commas or hyphens - The string method `d.join(list_of_words)` does the opposite of `split` and combines a list of words into a single string, delimited by `d` # Map, Filter, and Reduce # Self Checks # Lab Time