|
1 | 1 | ---
|
2 | 2 | Title: '.append()'
|
3 |
| -Description: 'Adds an item to end of the list.' |
| 3 | +Description: 'Adds a single item to the end of a list in Python.' |
4 | 4 | Subjects:
|
5 |
| - - 'Data Science' |
| 5 | + - 'Code Foundations' |
6 | 6 | - 'Computer Science'
|
7 | 7 | Tags:
|
| 8 | + - 'Arrays' |
| 9 | + - 'Data Structures' |
8 | 10 | - 'Lists'
|
9 | 11 | - 'Methods'
|
10 | 12 | CatalogContent:
|
11 | 13 | - 'learn-python-3'
|
12 |
| - - 'paths/data-science' |
13 | 14 | - 'paths/computer-science'
|
14 | 15 | ---
|
15 | 16 |
|
16 |
| -The Python list method `.append()` adds an item to the end of a list. |
| 17 | +The **`.append()`** method adds a single item to the end of an existing Python list. [Lists](https://www.codecademy.com/resources/docs/python/lists) in Python are mutable sequences that can store multiple items of different [data types](https://www.codecademy.com/resources/docs/python/data-types). When new elements need to be added to a list after it's been created, the `.append()` method provides a simple and efficient way to add items to the end of the list. |
17 | 18 |
|
18 | 19 | ## Syntax
|
19 | 20 |
|
20 |
| -```py |
| 21 | +```pseudo |
21 | 22 | list.append(item)
|
22 | 23 | ```
|
23 | 24 |
|
24 |
| -The `.append()` will place the object passed in as a new element at the very end of the list. Printing the list afterwards will visually show the appended value. |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `item`: An element of any data type (string, number, list, etc.) to be added to the end of the list. |
| 28 | + |
| 29 | +**Return value:** |
25 | 30 |
|
26 |
| -This method is not to be confused with returning an entirely new list with the passed object. |
| 31 | +The method doesn't return any value (returns `None`). It modifies the original list in-place. |
27 | 32 |
|
28 |
| -## Example |
| 33 | +## Example 1: Adding an item to a list |
29 | 34 |
|
30 |
| -To add `'🥚'` to the end of the `grocery` list: |
| 35 | +This example demonstrates how to add an item to the end of a list: |
31 | 36 |
|
32 | 37 | ```py
|
33 |
| -grocery = ['🍉', '🍪', '🥬', '🥕'] |
| 38 | +# Create a list of fruits |
| 39 | +fruits = ['apple', 'banana', 'cherry'] |
| 40 | + |
| 41 | +# Add 'orange' to the list |
| 42 | +fruits.append('orange') |
| 43 | + |
| 44 | +# Print the updated list |
| 45 | +print(fruits) |
| 46 | +``` |
34 | 47 |
|
35 |
| -grocery.append('🥚') |
| 48 | +This example results in the following output: |
36 | 49 |
|
37 |
| -print(grocery) |
38 |
| -# Output: ['🍉', '🍪', '🥬', '🥕', '🥚'] |
| 50 | +```shell |
| 51 | +['apple', 'banana', 'cherry', 'orange'] |
39 | 52 | ```
|
40 | 53 |
|
41 |
| -## Codebyte Example |
| 54 | +The original list `fruits` has been modified to include `'orange'` as the last element. The `.append()` method added the new item directly to the end of the list. |
| 55 | + |
| 56 | +## Example 2: Adding list to a list |
| 57 | + |
| 58 | +When appending a list to another list, the entire list is added as a single element: |
| 59 | + |
| 60 | +```py |
| 61 | +# Create a list of fruits |
| 62 | +fruits = ['apple', 'banana', 'cherry'] |
| 63 | + |
| 64 | +# Create a list of berries |
| 65 | +berries = ['strawberry', 'blueberry'] |
42 | 66 |
|
43 |
| -To add `'tulips'` to the end of the `orders` list: |
| 67 | +# Append the berries list to the fruits list |
| 68 | +fruits.append(berries) |
| 69 | + |
| 70 | +# Print the updated list |
| 71 | +print(fruits) |
| 72 | +``` |
| 73 | + |
| 74 | +This example will generate the following output: |
| 75 | + |
| 76 | +```shell |
| 77 | +['apple', 'banana', 'cherry', ['strawberry', 'blueberry']] |
| 78 | +``` |
| 79 | +
|
| 80 | +Notice that the `berries` list has been added as a single item to the `fruits` list, creating a nested list structure. To add each element individually instead, the [`.extend()`](https://www.codecademy.com/resources/docs/python/lists/extend) method would be more appropriate. |
| 81 | +
|
| 82 | +## Codebyte example: Demonstrating `.append()` in Python |
| 83 | +
|
| 84 | +This example demonstrates how to use `.append()` to add various items including strings, numbers, and lists to a Python list: |
44 | 85 |
|
45 | 86 | ```codebyte/python
|
46 |
| -orders = ['daisies', 'periwinkle'] |
| 87 | +# Create an empty shopping list |
| 88 | +shopping_list = [] |
| 89 | + |
| 90 | +# Add items to the shopping list |
| 91 | +shopping_list.append('bread') |
| 92 | +shopping_list.append('milk') |
| 93 | +shopping_list.append('eggs') |
| 94 | + |
| 95 | +print("Shopping list:", shopping_list) |
47 | 96 |
|
48 |
| -orders.append('tulips') |
| 97 | +# Add a numeric item to the list |
| 98 | +shopping_list.append(3) # Number of apples |
49 | 99 |
|
50 |
| -print(orders) |
| 100 | +print("Updated shopping list:", shopping_list) |
| 101 | + |
| 102 | +# Append a nested list |
| 103 | +shopping_list.append(['cheese', 'butter']) |
| 104 | + |
| 105 | +print("Final shopping list:", shopping_list) |
| 106 | + |
| 107 | +# Access the nested list items |
| 108 | +print("Dairy items:", shopping_list[4]) |
| 109 | +``` |
| 110 | +
|
| 111 | +## Frequently Asked Questions |
| 112 | +
|
| 113 | +### 1. What is `.append()` and `.extend()` in Python? |
| 114 | +
|
| 115 | +`.append()` adds a single item to the end of a list as one element, even if that item is itself a list. `.extend()` adds individual elements from an iterable (like another list) to the end of the current list. For example: |
| 116 | +
|
| 117 | +```py |
| 118 | +list1 = [1, 2, 3] |
| 119 | +list1.append([4, 5]) # Results in [1, 2, 3, [4, 5]] |
| 120 | +list2 = [1, 2, 3] |
| 121 | +list2.extend([4, 5]) # Results in [1, 2, 3, 4, 5] |
| 122 | +``` |
| 123 | +
|
| 124 | +### 2. When can I use append? |
| 125 | +
|
| 126 | +You can use `.append()` whenever you need to add a single item to the end of an existing list. Common use cases include: |
| 127 | +
|
| 128 | +- Building a list dynamically as you collect data |
| 129 | +- Adding user input to a list |
| 130 | +- Accumulating results in a calculation |
| 131 | +- Constructing a list step by step in algorithms |
| 132 | +
|
| 133 | +### 3. How to append a string? |
| 134 | +
|
| 135 | +Appending a string to a list works just like appending any other item. The string becomes a single element in the list: |
| 136 | +
|
| 137 | +```py |
| 138 | +words = ['hello', 'world'] |
| 139 | +words.append('python') # Results in ['hello', 'world', 'python'] |
| 140 | +``` |
| 141 | +
|
| 142 | +If you want to append each character of a string separately, you can use the `.extend()` method instead: |
| 143 | +
|
| 144 | +```py |
| 145 | +letters = ['a', 'b', 'c'] |
| 146 | +letters.extend('def') # Results in ['a', 'b', 'c', 'd', 'e', 'f'] |
51 | 147 | ```
|
0 commit comments