How to Modify the Elements of a List in Python

Spread the love

In our previous post, we talked about how to create and access elements in a list and in this post, we will talk about modifying elements of a list in Python.

Modify Elements of a List in Python –

To modify the elements of a list we write the name of the list followed by the index of the element that you want to change and then provide the new value that you want that item to have.

Here we have a list of company names.

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

And let’s say you want to change Facebook to Twitter. To do this you will write

In [20]: companies[3] = 'Twitter'

In [21]: companies
Out[21]: ['Google', 'Microsoft', 'Apple', 'Twitter', 'Netflix', 'Airbnb']

Rating: 1 out of 5.

Leave a Reply