Click for All Topics
append()
method.Syntax –
list.append(item)
Parameters –
my_list=[10,20,30,40]
print("Original list: ",my_list)
my_list.append(50)
print("My list after appending 50: ",my_list)
# Output -
# Original list: [10, 20, 30, 40]
# My list after appending 50: [10, 20, 30, 40, 50]
.append()
method.
Adding a list to the list – you can also add a list to the list using the .append()
method.
my_list=['edSlash', 'official']
print("Original list : ",my_list)
my_list.append(['Data Science','Gwalior'])
print("My list after appending another list :",my_list)
# Output -
# Original list : ['edSlash', 'official']
# My list after appending another list : ['edSlash', 'official', ['Data Science','Gwalior']]
Example 3 – add all even elements to the list
l=[]
print("Original list :",l)
for i in range(20):
if i%2==0:
l.append(i)
print('List after appending all even elements in a range of 20')
print(l)
# Output -
# Original list : []
# List after appending all even elements in a range of 20
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Example 4 – append a dictionary to the list
lst=['a','b','c','d']
print("Original list :",lst)
dct={'a':1,'b':2,'c':3}
lst.append(dct)
print("Modified original list :",lst)
# Output -
# Original list : ['a', 'b', 'c', 'd']
# Modified original list : ['a', 'b', 'c', 'd', {'a': 1, 'b': 2, 'c': 3}]
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India