Python List.append()
Python List append() method appends element to the end of this list.
The syntax to use append() function to add element at the end of the list is
myList.append(element)
append() method modifies the source list.
append() method returns None.
Example – List.append()
In the following program, we define a list of elements [52, 41, 63]
. Then, we append an element 98
to this list using append() method.
Python Program
myList = [52, 41, 63]
myList.append(98)
print(myList)
Run Output
[52, 41, 63, 98]
Append Another List to this List
You can use append() function to append another list of element to this list.
In the following program, we shall use for loop to iterate over elements of second list and append each of these elements to the first list.
Python Program
list_1 = [52, 41, 63]
list_2 = [47, 27, 97]
for element in list_2:
list_1.append(element)
print(list_1)
Run Output
[52, 41, 63, 47, 27, 97]
Summary
In this tutorial of Python Examples, we learned how to use List.append() method to append an element to the list.
Related Tutorials
- Python – Traverse List except Last Element
- Python – How to Create an Empty List?
- Python List with First N Elements
- Python List of Lists
- How to Access List Items in Python?
- How to Get List of all Files in Directory and Sub-directories?
- Python – Get Index or Position of Item in List
- Python List of Functions
- Python – Check if List Contains all Elements of Another List
- How to Check if Python List is Empty?