There’s no append() or extend() method for tuples in Python. Tuples are immutable data types so you can’t remove the element from it. However, you can extend the tuple using concatenate or slice method.
Python extend tuple example
Simple example code to will not extend tuples but form new tuples:
tup1 = (1, 2, 3) tup2 = tup1 + (4, 5, 6) print(tup2)
Using slice notation
Remove an element from a tuple
tup1 = (1, 2, 3) res = tup1[1:] print(res)
Output: (2, 3)
Build tuple from existing values:
name = "John" age = 30 location = "New York" p1 = (name, age, location) print(p1)
Output: (‘John’, 30, ‘New York’)
Do comment if you have any doubts or suggestions on this Python tuple program.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.