Python Update list In Hindi

📔 : Python 🔗

जैसा आपने पिछले topic में पढ़ा कि list items changeable होते हैं। list items को किस - किस तरह से update कर सकते हैं वो इस topic में देखेंगे।


list item को आप item index की help से भी access करके और उन्हें एक new value assign करके भी assign कर सकते हैं।

Python Update list Example

Copy Fullscreen Close Fullscreen Run
l = ['apple', 'banana', 'mango', 'black berry', 'plum']
print('before change : ', l)

#change 1st, 2nd item.
l[0] = 'papaya'
l[1] = 'grapes'
print('After updating list item : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_update.py
before change :  ['apple', 'banana', 'mango', 'black berry', 'plum']
After updating list item :  ['papaya', 'grapes', 'mango', 'black berry', 'plum']

Python Update Each item of list

आप for loop या while loop का use करके list के हर एक item को update कर सकते हैं -

Copy Fullscreen Close Fullscreen Run
l = [1,2,3,4,5,7,8,9,10]
print('Before calculate : ', l)

#now access each item of list and multiply by 2.
index = 0
while index < len(l) :
    l[index] = l[index] * 2
    index = index + 1
	
print('After calculate : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_update.py
Before calculate :  [1, 2, 3, 4, 5, 7, 8, 9, 10]
After calculate :  [2, 4, 6, 8, 10, 14, 16, 18, 20]

Python del keyword

del keyword का use करके हम किसी particular index से item delete करते हैं।

Copy Fullscreen Close Fullscreen Run
l = ['Apple', 'Banana', 'Papaya', 'Grapes', 'Black Berry', 'Berry', 'Plum']
print('Before deleting : ', l)

#remove 1st item.
del l[0]
print('After deleting 1st item : ', l)

#remove 5th item.
del l[4]
print('After deleting 4th item : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_del.py
Before deleting :  ['Apple', 'Banana', 'Papaya', 'Grapes', 'Black Berry', 'Berry', 'Plum']
After deleting 1st item :  ['Banana', 'Papaya', 'Grapes', 'Black Berry', 'Berry', 'Plum']
After deleting 5th item :  ['Banana', 'Papaya', 'Grapes', 'Black Berry', 'Plum']

Python pop()

By default pop() function list का last item remove करता है , लेकिन index number pass करने पर वह उस particular index का item भी remove देता है।

Copy Fullscreen Close Fullscreen Run
l = ['Banana', 'Papaya', 'Grapes', 'Black Berry', 'Plum']
print('Before deleting : ', l)

#using pop() function : it removes last item.
l.pop()
print('After removing last item : ', l)

#remove from a particular element : it will remove Black Berry from list.
l.pop(3)
print('After removing 3rd index item : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_pop.py
Before deleting :  ['Banana', 'Papaya', 'Grapes', 'Black Berry', 'Plum']
After removing last item :  ['Banana', 'Papaya', 'Grapes', 'Black Berry']
After removing 3rd index item :  ['Banana', 'Papaya', 'Grapes']

Python remove()

अब अगर आप value के according item delete करना चाहते हैं तो ,remove() function का use कर सकते हैं जिसमे जिस item को remove करना हो उसे pass करते हैं।

Copy Fullscreen Close Fullscreen Run
l = ['Banana', 'Papaya', 'Grapes']
print('Before deleting : ', l)

#using remove() function : it removes given item.
l.remove('Papaya')
print('After removing Papaya : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_remove.py
Before deleting :  ['Banana', 'Papaya', 'Grapes']
After removing Papaya :  ['Banana', 'Grapes']

list को एक बार में clear करने के लिए clear() function का use कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
l = ['Banana', 'Papaya', 'Grapes']
print('Before clear : ', l)
l.clear();
print('After clear : ', l)
Output
C:\Users\Rahulkumar\Desktop\python>python list_remove.py
Before clear :  ['Banana', 'Papaya', 'Grapes']
After clear :  []

I Hope, अब आप समझ गए होंगे कि list से किस किस तरह से list items को update कर सकते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers