Python List append()

Previous
Next

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

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.

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:

[‘item1’, ‘item2’, ‘item3’, ‘addedItem1’]
[‘item1’, ‘item2’, ‘item3’, ‘addedItem1’, 1, 2]

You can add any python data such as list, set and tuple to the Python list using append method.

l=['item1','item2','item3'] l.append(('item4','item5','item6')) print(l)

Output:

[‘item1’, ‘item2’, ‘item3’, (‘item4’, ‘item5’, ‘item6’)]

As you can see we have added tuple (‘item4’, ‘item5’, ‘item6’) as it is.You can add set too the list.

l=['item1','item2','item3'] l.append(('item4','item5','item6')) l.append({1,2,3}) print(l)

Output:

[‘item1’, ‘item2’, ‘item3’, (‘item4’, ‘item5’, ‘item6’), {1, 2, 3}]

That’s all about Python List append method.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog