background-image: url(../images/codecamp.png) background-color: #cacaca --- class: center, middle # Lists (Part 1) --- ## Lists As Ordered Collections Like strings, lists are a **collection data type** -- This also means they are non-primitive, or that they contain other data types --- ## List Values A list may contain _any_ value. Really. ANY value. -- This means we can do things like: ```python mixed_list = [1, 2, [3, 4]] ``` Here, `[3, 4]` is referred to as a **sublist** --- ## Stuff We Can Do With Lists ### Get the length ```python print(len(['a', 'b', 'c'])) mixed_list = [1, 2, [3, 4]] print(len(mixed_list)) ``` --- ## Stuff We Can Do With Lists List with strings, we can access elements of a list directly using **bracket notation** ```python some_letters = ['a', 'b', 'c'] print(some_letters[1]) ``` --- ## List Membership The `in` operator allows us to create boolean expressions that we can use to determine whether or not an item is in a list ```python some_letters = ['a', 'b', 'c'] print('a' in some_letters) print('d' in some_letters) print('d' not in some_letters) print(not 'd' in some_letters) ``` **Output**: `True` `False` `True` `True` --- ## Concatenation Like strings, we can use `+` to concatenate lists -- ```python combined = ['a', 'b', 'c'] + [1, 2, 3] print(combined) ``` **Output:** `['a', 'b', 'c', 1, 2, 3]` --- ## Repetition Similarly, we can use `*` to repeat lists -- ```python repeated = [1, 2, 3] * 3 print(repeated) ``` **Output:** [1, 2, 3, 1, 2, 3, 1, 2, 3]