python / beginner
Snippet
Adding Items to a List
The .append() method allows you to add a single element to the end of an existing list.
snippet.py
1
2
3
4
colors = ["red", "green"]colors.append("blue")print(colors) # Output: ['red', 'green', 'blue']
Breakdown
1
colors = ["red", "green"]
Initializes a list with two string elements.
2
colors.append("blue")
Modifies the list by placing 'blue' at the very end.