Python Update List in Hindi In Python Programming
Python Programming sikhne me asan hai aur iske syntax ko samajhna kafi easy hai. Aaj ke is article "Python Update List in Hindi" me hum detail me dekhenge ki kaise aap Python me list ko update kar sakte hain. List items changeable hote hain, iska matlab hai ki aap list ke items ko badal sakte hain, nayi values assign kar sakte hain aur unnecessary items ko remove bhi kar sakte hain. Ye article apko step-by-step samjhaye ga ki kaise aap Python me list ke saath different operations perform kar sakte hain.
Python Update List in Hindi | Python Programming In Hindi |
List Introduction
Python me list ek ordered collection hota hai jo multiple items ko ek sath store kar sakta hai. Lists mutable hoti hain, iska matlab hai ki aap list me items ko add, remove, aur update kar sakte hain. Lists ko square brackets [ ] ke sath define kiya jata hai aur har item comma , se separate hota hai.
यह भी पढ़ें: 250+ Free Python Projects with Source Code: From Beginner to Advanced
Example:
fruits = ['apple', 'banana', 'mango', 'blackberry', 'plum']
List Update Using Index
Aap list items ko unke index ke through access kar sakte hain aur unhe update kar sakte hain. Indexing zero-based hoti hai, matlab first item ka index 0 hoga, second ka 1, aur aise hi aage badhta hai.
Example:
l = ['apple', 'banana', 'mango', 'blackberry', 'plum']
print('before change : ', l)
#change 1st, 2nd item.
l[0] = 'papaya'
l[1] = 'grapes'
print('After updating list item : ', l)
Output:
before change : ['apple', 'banana', 'mango', 'blackberry', 'plum']
After updating list item : ['papaya', 'grapes', 'mango', 'blackberry', 'plum']
Update Each List Item Using Loops
Agar aapko list ke har item ko update karna ho, to aap for loop ya while loop ka use kar sakte hain. Niche ek example diya gaya hai jo har item ko 2 se multiply karta hai.
Example:
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:
Before calculate : [1, 2, 3, 4, 5, 7, 8, 9, 10]
After calculate : [2, 4, 6, 8, 10, 14, 16, 18, 20]
Deleting List Items Using del Keyword
del keyword ka use karke aap kisi particular index se item delete kar sakte hain. Niche example me, pehle first item aur phir fifth item ko delete kiya gaya hai.
Example:
l = ['Apple', 'Banana', 'Papaya', 'Grapes', 'Blackberry', '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:
Before deleting : ['Apple', 'Banana', 'Papaya', 'Grapes', 'Blackberry', 'Berry', 'Plum']
After deleting 1st item : ['Banana', 'Papaya', 'Grapes', 'Blackberry', 'Berry', 'Plum']
After deleting 5th item : ['Banana', 'Papaya', 'Grapes', 'Blackberry', 'Plum']
Removing Items with pop() Function
pop() function by default list ka last item remove karta hai, lekin agar aap index specify karein to wo us particular index se item remove karega.
Example:
l = ['Banana', 'Papaya', 'Grapes', 'Blackberry', '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 Blackberry from list.
l.pop(3)
print('After removing 3rd index item : ', l)
Output:
Before deleting : ['Banana', 'Papaya', 'Grapes', 'Blackberry', 'Plum']
After removing last item : ['Banana', 'Papaya', 'Grapes', 'Blackberry']
After removing 3rd index item : ['Banana', 'Papaya', 'Grapes']
Removing Specific Values with remove()
Agar aapko value ke according item delete karna ho, to remove() function ka use kar sakte hain. Isme aapko wo value pass karni hoti hai jo aapko list se remove karni hai.
Example:
l = ['Banana', 'Papaya', 'Grapes']
print('Before deleting : ', l)
#using remove() function : it removes given item.
l.remove('Papaya')
print('After removing Papaya : ', l)
Output:
Before deleting : ['Banana', 'Papaya', 'Grapes']
After removing Papaya : ['Banana', 'Grapes']
Clearing the Entire List
Agar aapko puri list clear karni ho to clear() function ka use kar sakte hain. Isse list ke sabhi items remove ho jate hain aur list empty ho jati hai.
Example:
l = ['Banana', 'Papaya', 'Grapes']
print('Before clear : ', l)
l.clear()
print('After clear : ', l)
Output:
Before clear : ['Banana', 'Papaya', 'Grapes']
After clear : []
Practical Examples and Tips
Python me list operations perform karna kafi straightforward hai. Niche kuch practical tips aur examples diye gaye hain jo apki coding skills ko enhance karenge.
Tip 1: List Slicing
List slicing ka use karke aap list ke ek part ko access kar sakte hain.
l = ['a', 'b', 'c', 'd', 'e']
print(l[1:4]) # Output: ['b', 'c', 'd']
Tip 2: List Comprehension
List comprehension ka use karke aap ek new list create kar sakte hain existing list ke elements ko modify karke.
l = [1, 2, 3, 4, 5]
new_list = [x * 2 for x in l]
print(new_list) # Output: [2, 4, 6, 8, 10]
Tip 3: Check If Item Exists
Aap in keyword ka use karke check kar sakte hain ki koi item list me exist karta hai ya nahi.
l = ['apple', 'banana', 'mango']
if 'banana' in l:
print('Banana is in the list')
Tip 4: List Concatenation
Aap two lists ko + operator ka use karke concatenate kar sakte hain.
l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = l1 + l2
print(l3)
# Output: [1, 2, 3, 4, 5, 6]
Frequently Asked Questions
Q1: Python me list aur array me kya difference hai?
Lists general-purpose data structures hain jo different types ke elements store kar sakti hain. Arrays homogenous data structures hain jo sirf same type ke elements store kar sakti hain.
Q2: List ko sort kaise karein?
Aap sort() method ka use karke list ko ascending order me sort kar sakte hain.
l = [3, 1, 4, 2, 5]
l.sort()
print(l) # Output: [1, 2, 3, 4, 5]
Q3: Python me list ka size kaise pata karein?
Aap len() function ka use karke list ka size (number of elements) pata kar sakte hain.
l = ['apple', 'banana', 'mango']
print(len(l)) # Output: 3
Q4: List me duplicate items kaise remove karein?
Aap set data structure ka use karke list me se duplicate items remove kar sakte hain.
l = [1, 2, 2, 3, 4, 4, 5]
l = list(set(l))
print(l) # Output: [1, 2, 3, 4, 5]
Conclusion
I hope ab aap samajh gaye honge ki Python me list se kis tarah se different operations perform kar sakte hain jaise ki list ko update karna, items ko delete karna, aur list ko clear karna. Python Programming In Hindi me aap easily lists ko manage kar sakte hain aur apne programs ko aur bhi efficient bana sakte hain.
Agar aapko ye article pasand aaya ho to share zarur karein aur Python Programming ke aur bhi interesting topics explore karte rahein!
यह भी पढ़ें: Python Programming In Hindi | Python Tutorials In Hindi