Python Tutorial
Python Join Tuples
Join tuples with + . Multiply a tuple with * to repeat it.
Join and Multiply
+ concatenates. * repeats.
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)
print(tuple1 + tuple2)
print(tuple1 * 2)📘 Real-World Deep Dive
Joining tuples is where the "tuples are immutable" rule becomes concrete: you never edit a tuple, you build a new one. Getting comfortable with that is what stops beginners from fighting the language when they want a fixed, shareable record.
Real-Life Scenario
A drawing program stores a path as a tuple of (x, y) points. When the user extends the path, we produce a brand-new path tuple rather than mutating the old one — so undo/history keeps working.
Real-Life Example
def extend(path: tuple, *points: tuple) -> tuple:
return path + points # concatenation makes a NEW tuple
start = ((0, 0), (1, 2))
line = extend(start, (3, 4), (5, 0))
print("old:", start) # unchanged — safe to keep in history
print("new:", line)
print("len:", len(line))Because + returns a new tuple, the original stays intact — that immutability is exactly what makes undo/history reliable.
Expected Output
old: ((0, 0), (1, 2))
new: ((0, 0), (1, 2), (3, 4), (5, 0))
len: 4Common mistakes
+only joins tuple with tuple.(1, 2) + [3]raisesTypeError— convert the list first:(1, 2) + tuple([3]).(1)is just the integer 1, not a tuple. A one-element tuple needs the trailing comma:(1,). This bites people joining single items.- Building a big tuple by repeated
t = t + (x,)in a loop is O(n²) — accumulate in a list and calltuple(...)once at the end.
🚀 Performance & Best Practices
- Repeated concatenation copies every element each time. For many appends, use a list and convert once — one allocation instead of n.
sum(tuples, ())flattens a sequence of tuples but is also O(n²);itertools.chain.from_iterablethentuple()is the linear way.- Two tuples that never change can be shared freely across threads and used as dict keys — no defensive copying needed.
🧪 Try It Yourself
- Write
repeat(point, n)that returns a tuple with the same point n times, using*(e.g.(pt,) * n). - Flatten
((1, 2), (3, 4), (5,))into(1, 2, 3, 4, 5)withitertools.chain. - Explain why
extendcan safely be called from two places at once but a list-based version could not.