🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Add and Update Values from a Tuple in Python (Convert into List)
As you know: Tuples in Python are immutable. Once a tuple has been created, it cannot be modified afterwards or it would trigger an error: TypeError exception. That's what it costs if you want to increase security and speed when manipulating tuples!
my_tuple = ('red', 'green', 'blue')
my_tuple[0] = 'black'
Triggers TypeError: 'tuple' object does not support item assignment
○ How to Add and Update a Value from Tuple in Python?
Consequently, when you use this kind of data type, you are not supposed to add, update or remove items from a tuple. But, there is a workaround: You can convert the tuple into a list, change the list, then convert the list back into a tuple!
my_tuple = ('red', 'green', 'blue') # my_tuple = ('red', 'green', 'blue')
my_tuple = list(my_tuple) # my_tuple = ['red', 'green', 'blue']
my_tuple[0] = 'black' # my_tuple = ['black', 'green', 'blue']
my_tuple = tuple(my_tuple) # my_tuple = ('black', 'green', 'blue')
Note: The tuple immutability is applicable only to the Top level of the tuple itself, not to its contents. For example, a list inside a tuple can be changed as usual with indexing, without even converting the tuple into a list first.
my_tuple = (1, [2, 3], 4)
my_tuple[1][0] = 'abc'
my_tuple = (1, ['abc', 3], 4)
Let's play this video, stick around and watch until the end of this video! 👍🏻
Digital Academy™ 🎓
***
☞ WATCH NEXT:
○ Data Types in Python - • DATA TYPES in Python (Numbers, String...
○ Operators in Python - • OPERATORS in Python (Arithmetic, Assi...
○ IF Statements in Python - • CONDITIONAL Statements in Python (IF,...
○ FOR Loops in Python - • FOR Loop in Python (Syntax, Break, Co...
📖 Blog: http://digital.academy.free.fr/blog
📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners: • 🐍 Python 101: Learn Python Basics for...
📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...
🧑🎓 [COURSE] http://digital.academy.free.fr/courses
📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV
🛒 Shopping and Discounts: http://digital.academy.free.fr/store
💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ / digitalacademyy
✧ / digitalacademyfr
✧ / digital_academy_fr
✧ / digitalacademyonline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ / digital_academy
✧ https://www.buymeacoffee.com/digital_...
***