Python Update tuple In Hindi

📔 : Python 🔗

तो जैसा आपने पढ़ा कि Python में tuples unchangeable होते हैं means उन्हें update / edit या delete नहीं कर सकते हैं। अगर आप ऐसा करने की कोशिश करते भी हैं तो error आ जाती है।
For Example

t = ('Rahul', 34, 'Agra', 'India')
t[1] = 24

#TypeError: 'tuple' object does not support item assignment

फिर हम tuple को कैसे update करेंगे।


Python में tuples को आप direct update नहीं कर सकते हैं, लेकिन tuple को आप list में convert करके update कर सकते हैं। list() function की help से आपको पहले tuple को list में convert करना पड़ेगा then update करने के बाद फिर से tuple() function की help से tuple में convert करना पड़ेगा।

Python Update tuple Example

Copy Fullscreen Close Fullscreen Run
t = ('Rahul', 34, 'Agra', 'India')
print('Before update :', t)
#convert into list.
l = list(t)

#now we can update items.
l[0] = 'Rahul Kumar Rajput'
l[1] = 24

#again convert into tuple.
t = tuple(l)
print('After update', t)
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_update.py
Before update : ('Rahul', 34, 'Agra', 'India')
After update ('Rahul Kumar Rajput', 24, 'Agra', 'India')

Python Adding New Item In tuple

ठीक इसी तरह से अगर हमें किसी नए item को tuple में add करना होतो यही method use करेंगे।

Copy Fullscreen Close Fullscreen Run
t = ('Apple', 'Guava')
#convert into list.
print('Before append:', t)
l = list(t)

#now we can add items.
l.append('Mango')
l.append('Banana')
l.append('Grapes')

#again convert into tuple.
t = tuple(l)
print('After append :', t)  
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_append.py
Before append: ('Apple', 'Guava')
After append : ('Apple', 'Guava', 'Mango', 'Banana', 'Grapes')

किसी एक type के data को दुसरे type में convert करने को ही Type Casting कहते हैं।


ठीक इसी तरह से अगर tuple items delete कर सकते हैं , बाकी सभी कर सकते हैं जो list के साथ करते थे बस हमें type cast करना पड़ेगा।

Python Adding Two tuples

आप दो या दो से अधिक tuples को add करके एक single variable में merge कर सकते हैं।

Copy Fullscreen Close Fullscreen Run
t1 = ('Rahul Kumar', 'Grirsh')
t2 = ('Mohit', 'Gyansignh')
t3 = ('Pawan', 'Saurabh')
final_t = t1+t2+t3
print(final_t)
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_merge.py
('Rahul Kumar', 'Grirsh', 'Mohit', 'Gyansignh', 'Pawan', 'Saurabh')

Remember

tuple में जब कोई single element होता है तो उसका type tuple न होकर उस single item का ही type होता है। मतलब अगर parenthesis के अंदर सिर्फ एक ही element है , तो उसका type tuple नहीं होता है।
See Example -

Copy Fullscreen Close Fullscreen Run
t1 = (12)
print(type(t1))

t2 = ('Rahul')
print(type(t2))

t3 = (True)
print(type(t3))

t4 = (5.6)
print(type(t4))
Output
C:\Users\Rahulkumar\Desktop\python>python tuple_type.py
<class 'int'>
<class 'str'>
<class 'bool'>
<class 'float'>

इसलिए tuple पर कोई operation perform करने से पहले ये ध्यान रखें।

Python Deleting tuple

del keyword का use करके हम किसी भी tuple को completely delete कर सकते हैं।

t = (34,56,78)
del t
print(t) #It generates an error "NameError: name 't' is not defined" because now tuple doesn't exist

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