filmov
tv
Add and Update Values from a Tuple in Python (Convert into List) - Python Tutorial for Beginners

Показать описание
🎓 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:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ 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:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии