How to manipulate tuple in python.
Tuple can store multiple values, just like an array or list.
for example:
mytup = (1, 2, “3”, 4)
Just like an array, tuple can access values by their index. In our case, index 0 will return 1.
But unlike list, tuples are not mutable. Once a tuple is created, you cannot add or remove value from it. Which makes us think, how can we add or remove a value from a tuple?
There is one unusual way to manipulate tuple.
1. Create a tuple.
2. Convert tuple to a list.
3. Manipulate list.
4. Convert list back to tuple.
For example:
tup = (1, 2, 3, 4)
lt = list(tup)
lt.append(5)
tup = tuple(lt)
Here’s a short 1 minute video to better understand with live example:
Thanks and have a nice day :)