Table of Contents

    How to Update Lists in Python: A Comprehensive Guide

    Update List Using Index

    We can update list using index like below.

    list = ['English', 'Hindi']
    print ("Value available at index 1 : ", list[1])
    
    list[1] = 2001  #list[1]=2001 for single item update
    
    print ("New value available at index 1 : ", list[1])

    Output

    Value available at index 1 :  Hindi
    New value available at index 1 :  2001

    Update List Using slice

    We can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator.

    list = ['English', 'Hindi', 1997, 2000]
    print ("Value available at index 2 : ", list[2])
    
    list[2:3] = 2001,2002 #list[2]=2001 for single item update
    
    print ("New value available at index 2 : ", list[2])
    print ("New value available at index 3 : ", list[3])

    Output

    Value available at index 2 :  1997
    New value available at index 2 :  2001
    New value available at index 3 :  2002