top of page

Python Tutorial

Public·1 member

append() and extend() in Python

list.append(x)

Add an item to the end of the list; equivalent to a[len(a):] = [x].


Example:


my_list = [1,2,3,4,5]
my_list.append(6) # 6 will be appended at the end of list

Output: [1,2,3,4,5,6]


list.extend(L)

Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.


my_list = [1,2,3,4,5] 
ex = [5,6]
# ex list  will be appended at the end of list and increase size  by length of ex 
my_list.extend(ex)

Output: [1,2,3,4,5,5,6]



More on Lists

Python official website reference

:https://docs.python.org/2/tutorial/datastructures.html

10 Views
bottom of page