Python extend tuple | Example code

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 tuple but form new tuples:

Concatenate tuples

tup1 = (1, 2, 3)
tup2 = tup1 + (4, 5, 6)

print(tup2)

Output:

Python extend tuple

Using slice notation

Remove 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 and suggestions on this Python tuple program.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.