Appending new element into a list

Elements in a list are changeable or mutable that means elements can be deleted, adding new element at the end of a list or updating current value of element with new one.

Append means adding new element at the end of list. We can use append() method to perform this task.

Below is the cars list.

cars = ['Tata','Ford','Renault','Ferrari','Volkwagen']
print(cars)

OUTPUT

['Tata', 'Ford', 'Renault', 'Ferrari', 'Volkwagen']

We will add new element ‘Toyota’ at the end of the above list using append method.

Syntax for append() method :-  list_name.append(element)

cars.append('Toyota')
print(cars)

OUTPUT

['Tata', 'Ford', 'Renault', 'Ferrari', 'Volkwagen', 'Toyota']

Above output shows new element ‘Toyota’ has been added at the end of list.

Now in the below example, we have created numbers list and then appended new element in the list.

numbers = [10,20,30,40,50]
print(numbers)
numbers.append(60)
print(numbers)
numbers.append(70)
print(numbers)

OUTPUT

[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50, 60]
[10, 20, 30, 40, 50, 60, 70]

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s