--- title: Python Lists and Tuples description: In python, Lists are are one of the 4 data types in Python used to store collections of data. --- Lists are one of the 4 data types in Python used to store collections of data. A short comparison of the containers is shown below: | Feature | List | Tuple | Dictionary | Set | |------------------|---------|---------|-------------|-------------| | Mutable (Can be modified in place) | Yes | No | Yes (keys are immutable) | Yes | | Iterable (Can be use in for loop) | Yes | Yes | Yes | Yes | | Ordered (Can access by index, slicing) | Yes | Yes | No | No | | Duplicate Values | Allowed | Allowed | Not in keys | Not allowed | # Lists ## Getting values with indexes ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture[0] # 'table' >>> furniture[1] # 'chair' >>> furniture[2] # 'rack' >>> furniture[3] # 'shelf' ``` ## Negative indexes ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture[-1] # 'shelf' >>> furniture[-3] # 'chair' >>> f'The {furniture[-1]} is bigger than the {furniture[-3]}' # 'The shelf is bigger than the chair' ``` ## Getting sublists with Slices ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture[0:4] # ['table', 'chair', 'rack', 'shelf'] >>> furniture[1:3] # ['chair', 'rack'] >>> furniture[0:-1] # ['table', 'chair', 'rack'] >>> furniture[:2] # ['table', 'chair'] >>> furniture[1:] # ['chair', 'rack', 'shelf'] >>> furniture[:] # ['table', 'chair', 'rack', 'shelf'] ``` Slicing the complete list will perform a copy: ```python >>> spam2 = spam[:] # ['cat', 'bat', 'rat', 'elephant'] >>> spam.append('dog') >>> spam # ['cat', 'bat', 'rat', 'elephant', 'dog'] >>> spam2 # ['cat', 'bat', 'rat', 'elephant'] ``` ## Getting a list length with `len()` ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> len(furniture) # 4 ``` ## Changing values with indexes ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture[0] = 'desk' >>> furniture # ['desk', 'chair', 'rack', 'shelf'] >>> furniture[2] = furniture[1] >>> furniture # ['desk', 'chair', 'chair', 'shelf'] >>> furniture[-1] = 'bed' >>> furniture # ['desk', 'chair', 'chair', 'bed'] ``` ## Concatenation and Replication ```python >>> [1, 2, 3] + ['A', 'B', 'C'] # [1, 2, 3, 'A', 'B', 'C'] >>> ['X', 'Y', 'Z'] * 3 # ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z'] >>> my_list = [1, 2, 3] >>> my_list = my_list + ['A', 'B', 'C'] >>> my_list # [1, 2, 3, 'A', 'B', 'C'] ``` ## Using for loops with Lists ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> for item in furniture: ... print(item) # table # chair # rack # shelf ``` ## Getting the index in a loop with `enumerate()` ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> for index, item in enumerate(furniture): ... print('index', index, '- item:', item) # index: 0 - item: table # index: 1 - item: chair # index: 2 - item: rack # index: 3 - item: shelf ``` ## Loop in Multiple Lists with `zip()` (Optional) ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> price = [100, 50, 80, 40] >>> for item, amount in zip(furniture, price): ... print(f'The {item} costs ${amount}') # The table costs $100 # The chair costs $50 # The rack costs $80 # The shelf costs $40 ``` ## The `in` and `not in` operators ```python >>> 'rack' in ['table', 'chair', 'rack', 'shelf'] # True >>> 'bed' in ['table', 'chair', 'rack', 'shelf'] # False >>> 'bed' not in furniture # True >>> 'rack' not in furniture # False ``` ## The Multiple Assignment (Unpacking) Trick The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> table = furniture[0] >>> chair = furniture[1] >>> rack = furniture[2] >>> shelf = furniture[3] ``` You could type this line of code: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> table, chair, rack, shelf = furniture >>> table # 'table' >>> chair # 'chair' >>> rack # 'rack' >>> shelf # 'shelf' ``` The multiple assignment trick can also be used to swap the values in two variables: ```python >>> a, b = 'table', 'chair' >>> a, b = b, a >>> print(a) # chair >>> print(b) # table ``` ## The `index()` Method The `index` method allows you to find the index of a value by passing its name: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture.index('chair') # 1 ``` ## Adding Values ### `append()` `append` adds an element to the end of a `list`: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture.append('bed') >>> furniture # ['table', 'chair', 'rack', 'shelf', 'bed'] ``` ### `insert()` `insert` adds an element to a `list` at a given position: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture.insert(1, 'bed') >>> furniture # ['table', 'bed', 'chair', 'rack', 'shelf'] ``` ## Removing Values ### `del` `del` removes an item using the index: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> del furniture[2] >>> furniture # ['table', 'chair', 'shelf'] >>> del furniture[2] >>> furniture # ['table', 'chair'] ``` ### `remove()` `remove` removes an item with using actual value of it: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> furniture.remove('chair') >>> furniture # ['table', 'rack', 'shelf'] ``` :::info :bulb: If the value appears multiple times in the list, only the first instance of the value will be removed. ::: ## Sorting values with `sort()` ```python >>> numbers = [2, 5, 3.14, 1, -7] >>> numbers.sort() >>> numbers # [-7, 1, 2, 3.14, 5] furniture = ['table', 'chair', 'rack', 'shelf'] furniture.sort() furniture # ['chair', 'rack', 'shelf', 'table'] ``` You can also pass `True` for the `reverse` keyword argument to have `sort()` sort the values in reverse order: ```python >>> furniture.sort(reverse=True) >>> furniture # ['table', 'shelf', 'rack', 'chair'] ``` By default, string are sorted using ASCII order and if you need to sort the values in regular alphabetical order, pass `str.lower` for the key keyword argument in the `sort()` method call: ```python >>> letters = ['a', 'z', 'A', 'Z'] >>> letters.sort(key=str.lower) >>> letters # ['a', 'A', 'z', 'Z'] ``` You can use the built-in function `sorted` to return a new list: ```python >>> furniture = ['table', 'chair', 'rack', 'shelf'] >>> sorted(furniture) # ['chair', 'rack', 'shelf', 'table'] ``` ## Numerical functions There are a number of built-in functions that can be used on `lists` that allow you to quickly look through a `list` without writing your own loops: ```python >>> nums = [3, 41, 12, 9, 74, 15] >>> print(max(nums)) >>> print(min(nums)) >>> print(sum(nums)) # 74 # 3 # 154 ``` ## Comprehension (Like the [set-builder notation](https://en.wikipedia.org/wiki/Set-builder_notation)) List Comprehensions are a special kind of syntax that let us create lists out of other lists, and are incredibly useful when dealing with numbers and with one or two levels of nested for loops. This is how we create a new list from an existing collection with a For Loop: ```python >>> names = ['Charles', 'Susan', 'Patrick', 'George'] >>> new_list = [] >>> for n in names: ... new_list.append(n) ... >>> new_list # ['Charles', 'Susan', 'Patrick', 'George'] ``` And this is how we do the same with a List Comprehension: ```python >>> names = ['Charles', 'Susan', 'Patrick', 'George'] >>> new_list = [n for n in names] >>> new_list # ['Charles', 'Susan', 'Patrick', 'George'] ``` We can do the same with numbers: ```python >>> n = [(a, b) for a in range(1, 3) for b in range(1, 3)] >>> n # [(1, 1), (1, 2), (2, 1), (2, 2)] ``` ### Adding conditionals If we want `new_list` to have only the names that start with C, with a for loop, we would do it like this: ```python >>> names = ['Charles', 'Susan', 'Patrick', 'George', 'Carol'] >>> new_list = [] >>> for n in names: ... if n.startswith('C'): ... new_list.append(n) ... >>> print(new_list) # ['Charles', 'Carol'] ``` In a List Comprehension, we add the `if` statement at the end: ```python >>> new_list = [n for n in names if n.startswith('C')] >>> print(new_list) # ['Charles', 'Carol'] ``` To use an `if-else` statement in a List Comprehension: ```python >>> nums = [1, 2, 3, 4, 5, 6] >>> new_list = [num*2 if num % 2 == 0 else num for num in nums] >>> print(new_list) # [1, 4, 3, 8, 5, 12] ``` :::info :bulb: Note that most of the time method will modify `list` in place, while function will create a new `list` ::: # Tuples ## The Tuple data type :::info :bulb: The key difference between tuples and lists is that, while <code>tuples</code> are <i>immutable</i> objects, <code>lists</code> are <i>mutable</i>. This means that tuples cannot be changed while the lists can be modified. Tuples are more memory efficient than the lists. ::: ```python >>> furniture = ('table', 'chair', 'rack', 'shelf') >>> furniture[0] # 'table' >>> furniture[1:3] # ('chair', 'rack') >>> len(furniture) # 4 ``` The main way that tuples are different from lists is that tuples, like strings, are immutable. ## Converting between `list()` and `tuple()` ```python >>> tuple(['cat', 'dog', 5]) # ('cat', 'dog', 5) >>> list(('cat', 'dog', 5)) # ['cat', 'dog', 5] >>> list('hello') # ['h', 'e', 'l', 'l', 'o'] ``` # Keywords - **containers (容器)**: Structures that hold or organize multiple values, such as lists, tuples, and strings. - **methods (方法)**: Functions that are associated with an object and can be called on that object. - **sequence (序列)**: An ordered collection of items, where each item can be accessed by its position (index). - **list (串列)**: A mutable sequence type in Python that can store a collection of items with different data types. - **tuple (元組)**: An immutable sequence type in Python that can store a collection of items with different data types. - **elements (元素)**: The individual items or values contained within a container like a list or tuple. - **index (索引)**: The numerical position of an element within a sequence, starting from 0. - **subscript operator (下標運算子)**: The square brackets (`[]`) used to access elements in a container by index. - **slicing (切片)**: A method to retrieve a subset of a sequence by specifying a start, stop, and optionally a step. - **mutable (可變的)**: Describes an object that can be changed after it is created, like a list. - **item assignment (元素賦值)**: The process of changing the value of an element within a mutable container using its index. - **iteration by item (逐項迭代)**: The process of going through each element in a container one at a time. - **list comprehension (串列生成式)**: A concise way to create lists by applying an expression to each item in a sequence. - **immutable (不可變的)**: Describes an object that cannot be changed after it is created, like a tuple or string. - **reference (參照)**: A variable in Python stores a reference, which is the memory address of an object. It points to where the object is stored rather than containing the object itself. - **modify in-place (原地修改)**: Changing the content of a mutable object without creating a new object.