In this tutorial, we will see about Python List‘s append method.Python List append is used to add single element to end of the list.
Let’s understand about basic of python list first.
Python List is nothing but the collection of elements. You can add any data type to the list.
You can create simple list as below
1 2 3 |
l=['item1','item2','item3'] |
Python List append example
You can simply use append method to add elements to the end of the list.It does not return anything, just updates the list.
Let’s understand this with the help of simple example.
1 2 3 4 5 6 7 8 9 10 |
l=['item1','item2','item3'] l.append('addedItem1') print(l) #You can add any type of data to the list l.append(1) l.append(2) print(l) |
Output:
You can add any python data such as list, set and tuple to the Python list using append method.
1 2 3 4 5 |
l=['item1','item2','item3'] l.append(('item4','item5','item6')) print(l) |
Output:
As you can see we have added tuple (‘item4’, ‘item5’, ‘item6’) as it is.You can add set too the list.
1 2 3 4 5 6 |
l=['item1','item2','item3'] l.append(('item4','item5','item6')) l.append({1,2,3}) print(l) |
Output:
That’s all about Python List append method.