Append in Python – Adding Elements to the End of a List

Spread the love

Append In Python –

The simplest way to add an element to a list is using Append method in Python. When you append an item to a list, the new element is added to the end of the list.

Syntax of append method-

list.append(item)

Let’s create a list

In [22]: companies = ['Google','Microsoft','Apple']

Now suppose you want to add Facebook at the end of this list. To do that you have to write

In [23]: companies.append('Facebook')

In [24]: companies
Out[24]: ['Google', 'Microsoft', 'Apple', 'Facebook']

Adding a list –

You can also add a list to the end of a list using append.

In [25]: companies.append(['Netflix','Airbnb'])

In [26]: companies
Out[26]: ['Google', 'Microsoft', 'Apple', 'Facebook', ['Netflix', 'Airbnb']]

Rating: 1 out of 5.

Leave a Reply