Skip to content

Commit c4681e9

Browse files
[Edit] Python - .append() (#6573)
* Update append.md * updated broken link, spacing, backticks, bold terms, fixed lint and format * added codebyte description --------- Co-authored-by: Mamta Wardhani <[email protected]>
1 parent cc3c83d commit c4681e9

File tree

1 file changed

+114
-18
lines changed
  • content/python/concepts/lists/terms/append

1 file changed

+114
-18
lines changed
Lines changed: 114 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,147 @@
11
---
22
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.'
44
Subjects:
5-
- 'Data Science'
5+
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
8+
- 'Arrays'
9+
- 'Data Structures'
810
- 'Lists'
911
- 'Methods'
1012
CatalogContent:
1113
- 'learn-python-3'
12-
- 'paths/data-science'
1314
- 'paths/computer-science'
1415
---
1516

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.
1718

1819
## Syntax
1920

20-
```py
21+
```pseudo
2122
list.append(item)
2223
```
2324

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:**
2530

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.
2732

28-
## Example
33+
## Example 1: Adding an item to a list
2934

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:
3136

3237
```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+
```
3447

35-
grocery.append('🥚')
48+
This example results in the following output:
3649

37-
print(grocery)
38-
# Output: ['🍉', '🍪', '🥬', '🥕', '🥚']
50+
```shell
51+
['apple', 'banana', 'cherry', 'orange']
3952
```
4053

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']
4266

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:
4485
4586
```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)
4796

48-
orders.append('tulips')
97+
# Add a numeric item to the list
98+
shopping_list.append(3) # Number of apples
4999

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']
51147
```

0 commit comments

Comments
 (0)