|
| 1 | +Python allows you to manipulate `list` in a lot of ways. A `list` is simple a collection of objects. They are mutable, ordered and indexed. Let's look at the methods that are available to manipulate a `list` object. |
| 2 | + |
| 3 | +When you manipulate a list with a list-method, you are changing the properties of the list you pass. That is, **you will alter the list** object that is being used with the list-method. If you do not want to change the original list, you need to copy the list and then work on the copied list. |
| 4 | + |
| 5 | +To begin, you first need a `list` object to use the functions. |
| 6 | + |
| 7 | +```python |
| 8 | +ex_list = list() # (or) ex_list = [] |
| 9 | +print(ex_list) |
| 10 | +#=> [] |
| 11 | +``` |
| 12 | + |
| 13 | +## Add an Item to the List |
| 14 | + |
| 15 | +Topics to discuss: |
| 16 | + |
| 17 | +1. `append` |
| 18 | +2. `insert` |
| 19 | +3. `extend` |
| 20 | + |
| 21 | +If you want to add an item to an existing list, you use the list-method `append()` for it. As the name represent, it appends the item to the **end** of the list. |
| 22 | + |
| 23 | +```python |
| 24 | +ex_list = [1, 2, 3] |
| 25 | +ex_list.append(9) |
| 26 | +print(ex_list) |
| 27 | +#=> [1, 2, 3, 9] |
| 28 | +``` |
| 29 | + |
| 30 | +There is another way to add an item to the list. `insert()` method also gives you the ability to add the item to a particular index in the list. |
| 31 | + |
| 32 | +`insert` takes 2 parameters. |
| 33 | + |
| 34 | +1. index at which you want to add the item |
| 35 | +2. item |
| 36 | + |
| 37 | +Note: if the index is 0, the item will be added to the first of the list. if the index is more than or equal to the length of the list, then item will be added to the last of the list (like the append function) |
| 38 | + |
| 39 | +```python |
| 40 | +ex_list = [1, 2, 3] |
| 41 | +ex_list.insert(0, -2) |
| 42 | +print(ex_list) |
| 43 | +#=> [-2, 1, 2, 3] |
| 44 | +ex_list.insert(1, 0) |
| 45 | +print(ex_list) |
| 46 | +#=> [-2, 0, 1, 2, 3] |
| 47 | +``` |
| 48 | + |
| 49 | +If you have another list object who's content you need to be copied to your list, then you can use the `extend` function. |
| 50 | +The extend function takes an iterable as its parameter. |
| 51 | + |
| 52 | +```python |
| 53 | +ex_list = [1, 2, 3] |
| 54 | +extend_list = [5, 6, 7] |
| 55 | + |
| 56 | +ex_list.extend(extend_list) |
| 57 | +print(ex_list) |
| 58 | +#=> [1, 2, 3, 5, 6, 7] |
| 59 | + |
| 60 | +ex_list.extend([8, 9]) |
| 61 | +print(ex_list) |
| 62 | +#=> [1, 2, 3, 5, 6, 7, 8, 9] |
| 63 | +``` |
| 64 | + |
| 65 | +# Removing Items from the list |
| 66 | + |
| 67 | +Topics to discuss: |
| 68 | + |
| 69 | +1. `remove` |
| 70 | +2. `pop` |
| 71 | +3. `clear` |
| 72 | + |
| 73 | +If you want to remove an item from a list, you can use the `remove` function which takes one parameter. Feed the function with the item you want to remove from the list. the `remove` function will throw a `ValueError` if the item does not exist in the loop. |
| 74 | + |
| 75 | +```python |
| 76 | +ex_list = [1, 2, 3] |
| 77 | +ex_list.remove(2) |
| 78 | +print(ex_list) |
| 79 | +#=> [1, 3] |
| 80 | +ex_list.remove(0) |
| 81 | +#=> ValueError: list.remove(x): x not in list |
| 82 | +``` |
| 83 | + |
| 84 | +There is another way to remove an item from the list. If you know the index of the item which you want to remove, you can use `pop` function. Pop takes 1 parameter, the index of the item you need to remove, and return it to you. If you specify an index more than the length of the list, you will get an `IndexError`. If you dont specify an index, the function will remove the last element and return it to you. |
| 85 | + |
| 86 | +```python |
| 87 | +ex_list = [1, 2, 3] |
| 88 | +ex_list.pop(0) |
| 89 | +#=> 1 |
| 90 | +print(ex_list) |
| 91 | +#=> [2, 3] |
| 92 | +ex_list.pop() |
| 93 | +#=> [2] |
| 94 | +``` |
| 95 | + |
| 96 | +If you want to clear the list (ie: all the items in the list), then you can use the `clear` function. It does not take any parameter as input but will remove all the elements in the list. |
| 97 | + |
| 98 | +```python |
| 99 | +ex_list = [1, 2, 3] |
| 100 | +ex_list.clear() |
| 101 | +print(ex_list) |
| 102 | +#=> [] |
| 103 | +``` |
| 104 | + |
| 105 | +## Reverse the list |
| 106 | + |
| 107 | +The items in the list can be reordered in the reverse order with the `reverse` function. |
| 108 | + |
| 109 | +```python |
| 110 | +ex_list = [1, 2, 3] |
| 111 | +ex_list.reverse() |
| 112 | +print(ex_list) |
| 113 | +#=> [3, 2, 1] |
| 114 | +``` |
| 115 | + |
| 116 | +## Sort the list |
| 117 | + |
| 118 | +When you have an random ordered list of items, you can sort them with the help of `sort` function. If you have items that are alphanumerical, you dont have to provide any inputs to sort. |
| 119 | + |
| 120 | +ex: you have list of numbers, or you have a list of names and you want to sort them |
| 121 | + |
| 122 | +```python |
| 123 | +ex_list = ["Tony", "Natasha", "Thor", "Bruce"] |
| 124 | +ex_list.sort() |
| 125 | +print(ex_list) |
| 126 | +#=> ["Bruce", "Natasha", "Thor", "Tony"] |
| 127 | +``` |
| 128 | + |
| 129 | +If you want the sort to be in descending order, you can use the reverse parameter. |
| 130 | + |
| 131 | +```python |
| 132 | +ex_list = ["Tony", "Natasha", "Thor", "Bruce"] |
| 133 | +ex_list.sort(reverse=True) |
| 134 | +print(ex_list) |
| 135 | +#=> ["Tony", "Thor", "Natasha", "Bruce"] |
| 136 | +``` |
| 137 | + |
| 138 | +If you have a list of items that are complex, you can use the key parameter in the sort. Lets see more about this later. |
| 139 | + |
| 140 | +Internally, python uses `Timsort` to sort the list. |
| 141 | + |
| 142 | +## Occurances of an item in the list. |
| 143 | + |
| 144 | +You can find the occurances of an element in the list with the help of the `count` function. It takes one parameter which will be the item, for which you need to find the number of occurances. |
| 145 | + |
| 146 | +```python |
| 147 | +ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3] |
| 148 | +ex_list.count(1) |
| 149 | +#=> 3 |
| 150 | +``` |
| 151 | + |
| 152 | +## Find the Index of the Item. |
| 153 | + |
| 154 | +If you want to find the index of an item you want in the list, you can use the `index` function. If you have multiple occurances of the same item, it will provide you the index of the first occurance. If you do not have any occurances of the item, then you will have a `ValueError` raised. |
| 155 | + |
| 156 | +Index starts with 0. |
| 157 | + |
| 158 | +```python |
| 159 | +ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3] |
| 160 | +ex_list.index(4) |
| 161 | +#=> 1 |
| 162 | +ex_list.index(10) |
| 163 | +#=>ValueError: 10 is not in list |
| 164 | +``` |
| 165 | + |
| 166 | +You can also provide a start (and an end) to the index from where you need the search has to happen for the item. |
| 167 | + |
| 168 | +```python |
| 169 | +ex_list = [1, 4, 7, 8, 2, 9, 2, 1, 1, 0, 4, 3] |
| 170 | +ex_list.index(4, 2, 12) |
| 171 | +#=> 10 |
| 172 | +``` |
| 173 | + |
| 174 | +## Copy a List. |
| 175 | + |
| 176 | +Lists are a collection of items which are referenced to an index, Hence if you do an assignment of a list object to another variable. Any change you make to the assigned variable will also have an impact on the original variable. |
| 177 | + |
| 178 | +```python |
| 179 | +ex_list = ["Tony", "Natasha", "Thor", "Bruce"] |
| 180 | +new_list = ex_list |
| 181 | +new_list.append("Clarke") |
| 182 | +print(new_list) |
| 183 | +#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"] |
| 184 | +print(ex_list) |
| 185 | +#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"] |
| 186 | +``` |
| 187 | + |
| 188 | +You need to use the `copy` function in order to do a `shallow_copy` of the list. ( More about `shallow_copy` and `deep_copy` later.) |
| 189 | + |
| 190 | +```python |
| 191 | +ex_list = ["Tony", "Natasha", "Thor", "Bruce"] |
| 192 | +new_list = ex_list.copy() |
| 193 | +new_list.append("Clarke") |
| 194 | +print(new_list) |
| 195 | +#=> ["Tony", "Natasha", "Thor", "Bruce", "Clarke"] |
| 196 | +print(ex_list) |
| 197 | +#=> ["Tony", "Natasha", "Thor", "Bruce"] |
| 198 | +``` |
0 commit comments