Friday 18 December 2020

Python: Modify element at specific index

Modifying an existing element in the list is same as variable assignment. To modify/update an element in the list, use name of the list followed by an index of the element, followed by an equal operator and actual value.

 

Syntax

list_name[index_number] = value

 

Example

country_names[1] = "Nepal"

>>> country_names = ["India", "Australia", "Sri Lanka", "Itali"]
>>> 
>>> country_names
['India', 'Australia', 'Sri Lanka', 'Itali']
>>> 
>>> country_names[1] = "Nepal"
>>> 
>>> country_names
['India', 'Nepal', 'Sri Lanka', 'Itali']

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment