Important Notice:

Updating List Elements

Updating List Elements

7 views 1 min read
Updating List Elements :-
Python में Updating List Elements का अर्थ है List में मौजूद किसी existing element (मौजूदा element) की value को बदलना या उसे नई value से replace करना।

List एक Mutable Data Type है, इसलिए List बनने के बाद इसके elements को update किया जा सकता है।

English

Updating List Elements means changing or replacing the value of an existing element in a list.

A List is a Mutable Data Type, so its elements can be modified after the list is created.

Example:-

numbers = [10, 20, 30, 40, 50]

numbers[2] = 300

print(numbers)

Output:-

[10, 20, 300, 40, 50]

Because:-

यहाँ:

Element :  10   20   30   40   50
Index   :      0    1     2      3    4

numbers[2] = 300 का अर्थ है index 2 पर मौजूद 30 को 300 से replace करना।

इसलिए List बन जाती है:

[10, 20, 300, 40, 50]
 
Updating Multiple List Elements :-

एक साथ multiple elements को भी update किया जा सकता है।

numbers = [10, 20, 30, 40, 50]

numbers[1:4] = [200, 300, 400]

print(numbers)

Output:-

[10, 200, 300, 400, 50]

यहाँ slicing की सहायता से index 1 से 3 तक के elements update किए गए हैं।

Updating Using Negative Index :-

Negative index का उपयोग करके भी element को update किया जा सकता है।

numbers = [10, 20, 30, 40, 50]

numbers[-1] = 500

print(numbers)

Output:-

[10, 20, 30, 40, 500]

यहाँ -1 last element को represent करता है।

Important Point :-

List में element update करने के लिए index और assignment operator (=) का उपयोग किया जाता है।

list[index] = new_value

Example:

names = ["Rahul", "Amit", "Neha"]

names[1] = "Priya"

print(names)

Output:-

['Rahul', 'Priya', 'Neha']

यहाँ "Amit" को "Priya" से update किया गया है।

Related Notes