|
| 1 | +--- |
| 2 | +id: python-list |
| 3 | +title: List in Python |
| 4 | +sidebar_label: List in Python #displays in sidebar |
| 5 | +sidebar_position: 8 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + List in Python, |
| 10 | + Introduction of python, |
| 11 | + Python Syntax, |
| 12 | + Variables, |
| 13 | + Operators, |
| 14 | + Type Casting, |
| 15 | + String |
| 16 | + ] |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | + |
| 21 | +# Python Lists |
| 22 | + |
| 23 | +A **List** in Python is a data structure that allows you to store multiple items in a single variable. Lists are **ordered**, **mutable**, and **can contain elements of different data types**. |
| 24 | + |
| 25 | + |
| 26 | +## Creating a List |
| 27 | + |
| 28 | +You create a list using square brackets `[]`: |
| 29 | + |
| 30 | +```python |
| 31 | +# Empty List |
| 32 | +empty_list = [] |
| 33 | + |
| 34 | +# List of Integers |
| 35 | +numbers = [1, 2, 3, 4, 5] |
| 36 | + |
| 37 | +# List of Strings |
| 38 | +fruits = ["apple", "banana", "cherry"] |
| 39 | + |
| 40 | +# Mixed Data Types |
| 41 | +mixed = [1, "hello", 3.14, True] |
| 42 | +```` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +## Indexing |
| 47 | + |
| 48 | +**Indexing** means accessing elements by their position. |
| 49 | + |
| 50 | +* Index starts from **0** in Python: |
| 51 | + |
| 52 | +```python |
| 53 | +fruits = ["apple", "banana", "cherry"] |
| 54 | + |
| 55 | +print(fruits[0]) # apple |
| 56 | +print(fruits[1]) # banana |
| 57 | +print(fruits[2]) # cherry |
| 58 | +``` |
| 59 | + |
| 60 | +* Negative indexing starts from the end: |
| 61 | + |
| 62 | +```python |
| 63 | +print(fruits[-1]) # cherry |
| 64 | +print(fruits[-2]) # banana |
| 65 | +print(fruits[-3]) # apple |
| 66 | +``` |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## Slicing |
| 71 | + |
| 72 | +**Slicing** lets you extract a sublist: |
| 73 | + |
| 74 | +```python |
| 75 | +numbers = [10, 20, 30, 40, 50] |
| 76 | + |
| 77 | +print(numbers[1:4]) # [20, 30, 40] |
| 78 | +print(numbers[:3]) # [10, 20, 30] |
| 79 | +print(numbers[2:]) # [30, 40, 50] |
| 80 | +print(numbers[-3:-1]) # [30, 40] |
| 81 | +``` |
| 82 | + |
| 83 | +**Syntax:** |
| 84 | + |
| 85 | +``` |
| 86 | +list[start:stop:step] |
| 87 | +``` |
| 88 | + |
| 89 | +**Example with step:** |
| 90 | + |
| 91 | +```python |
| 92 | +print(numbers[::2]) # [10, 30, 50] |
| 93 | +``` |
| 94 | + |
| 95 | + |
| 96 | +## Modifying Elements |
| 97 | + |
| 98 | +Lists are **mutable**, which means you can change their contents: |
| 99 | + |
| 100 | +```python |
| 101 | +fruits = ["apple", "banana", "cherry"] |
| 102 | +fruits[1] = "mango" |
| 103 | +print(fruits) # ['apple', 'mango', 'cherry'] |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +## List Methods |
| 108 | + |
| 109 | +Python provides many built-in methods for lists: |
| 110 | + |
| 111 | +| Method | Description | |
| 112 | +| -------------- | ----------------------------------------------------- | |
| 113 | +| `append(x)` | Adds an item to the end of the list | |
| 114 | +| `insert(i, x)` | Inserts an item at a specific index | |
| 115 | +| `extend(iter)` | Adds all elements from another iterable | |
| 116 | +| `remove(x)` | Removes the first occurrence of the item | |
| 117 | +| `pop([i])` | Removes and returns the item at the given index | |
| 118 | +| `clear()` | Removes all elements | |
| 119 | +| `index(x)` | Returns the index of the first occurrence of the item | |
| 120 | +| `count(x)` | Counts how many times the item appears | |
| 121 | +| `sort()` | Sorts the list in ascending order | |
| 122 | +| `reverse()` | Reverses the list | |
| 123 | +| `copy()` | Returns a shallow copy of the list | |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +### Examples |
| 128 | + |
| 129 | +#### append() |
| 130 | + |
| 131 | +```python |
| 132 | +nums = [1, 2, 3] |
| 133 | +nums.append(4) |
| 134 | +print(nums) # [1, 2, 3, 4] |
| 135 | +``` |
| 136 | + |
| 137 | +#### insert() |
| 138 | + |
| 139 | +```python |
| 140 | +nums.insert(1, 100) |
| 141 | +print(nums) # [1, 100, 2, 3, 4] |
| 142 | +``` |
| 143 | + |
| 144 | +#### extend() |
| 145 | + |
| 146 | +```python |
| 147 | +nums.extend([5, 6]) |
| 148 | +print(nums) # [1, 100, 2, 3, 4, 5, 6] |
| 149 | +``` |
| 150 | + |
| 151 | +#### remove() and pop() |
| 152 | + |
| 153 | +```python |
| 154 | +nums.remove(100) |
| 155 | +print(nums) # [1, 2, 3, 4, 5, 6] |
| 156 | + |
| 157 | +nums.pop() # Removes the last element |
| 158 | +print(nums) # [1, 2, 3, 4, 5] |
| 159 | + |
| 160 | +nums.pop(2) # Removes index 2 |
| 161 | +print(nums) # [1, 2, 4, 5] |
| 162 | +``` |
| 163 | + |
| 164 | + |
| 165 | +## Iterating Through a List |
| 166 | + |
| 167 | +**Using a for loop:** |
| 168 | + |
| 169 | +```python |
| 170 | +fruits = ["apple", "banana", "cherry"] |
| 171 | + |
| 172 | +for item in fruits: |
| 173 | + print(item) |
| 174 | +``` |
| 175 | + |
| 176 | +**Output:** |
| 177 | + |
| 178 | +``` |
| 179 | +apple |
| 180 | +banana |
| 181 | +cherry |
| 182 | +``` |
| 183 | + |
| 184 | +**Using indices:** |
| 185 | + |
| 186 | +```python |
| 187 | +for i in range(len(fruits)): |
| 188 | + print(i, fruits[i]) |
| 189 | +``` |
| 190 | + |
| 191 | + |
| 192 | +## Membership Test |
| 193 | + |
| 194 | +Check whether an item exists in the list: |
| 195 | + |
| 196 | +```python |
| 197 | +print("apple" in fruits) # True |
| 198 | +print("mango" not in fruits) # True |
| 199 | +``` |
| 200 | + |
| 201 | + |
| 202 | +## Nested Lists |
| 203 | + |
| 204 | +Lists can contain other lists: |
| 205 | + |
| 206 | +```python |
| 207 | +matrix = [ |
| 208 | + [1, 2, 3], |
| 209 | + [4, 5, 6], |
| 210 | + [7, 8, 9] |
| 211 | +] |
| 212 | + |
| 213 | +print(matrix[0]) # [1, 2, 3] |
| 214 | +print(matrix[1][2]) # 6 |
| 215 | +``` |
| 216 | + |
| 217 | + |
| 218 | +## List Comprehensions |
| 219 | + |
| 220 | +A **concise way** to create new lists: |
| 221 | + |
| 222 | +```python |
| 223 | +squares = [x**2 for x in range(1, 6)] |
| 224 | +print(squares) # [1, 4, 9, 16, 25] |
| 225 | +``` |
| 226 | + |
| 227 | +**With a condition:** |
| 228 | + |
| 229 | +```python |
| 230 | +even = [x for x in range(10) if x % 2 == 0] |
| 231 | +print(even) # [0, 2, 4, 6, 8] |
| 232 | +``` |
| 233 | + |
| 234 | + |
| 235 | +## Copying Lists |
| 236 | + |
| 237 | +Be careful! Assigning directly creates a reference: |
| 238 | + |
| 239 | +```python |
| 240 | +a = [1, 2, 3] |
| 241 | +b = a |
| 242 | +b.append(4) |
| 243 | + |
| 244 | +print(a) # [1, 2, 3, 4] |
| 245 | +``` |
| 246 | + |
| 247 | +To create an **independent copy:** |
| 248 | + |
| 249 | +```python |
| 250 | +c = a.copy() |
| 251 | +c.append(5) |
| 252 | + |
| 253 | +print(a) # [1, 2, 3, 4] |
| 254 | +print(c) # [1, 2, 3, 4, 5] |
| 255 | +``` |
| 256 | + |
| 257 | + |
| 258 | +## Conclusion |
| 259 | + |
| 260 | +Python Lists are a **powerful and flexible** data structure used everywhere—from collecting and processing data to building complex programs. Practice using list methods and experiment to become confident. |
0 commit comments