Ei vielä käännetty
Tätä sivua ei ole vielä käännetty suomeksi, joten se näytetään englanniksi. Auta kääntämään
tuple() Function Complexity¶
The tuple() function creates tuples from iterables or creates empty tuples.
Complexity Analysis¶
| Case | Time | Space | Notes |
|---|---|---|---|
| Empty tuple | O(1) | O(1) | tuple() |
| From iterable | O(n) | O(n) | n = iterable length |
| From string | O(n) | O(n) | n = string length |
| From tuple (exact) | O(1) | O(1) | Returns same object |
| From list | O(n) | O(n) | Creates new tuple |
Basic Usage¶
Create Empty Tuple¶
# O(1)
t = tuple() # ()
From List¶
# O(n) - where n = list length
t = tuple([1, 2, 3]) # (1, 2, 3)
t = tuple([1, 2, 3, 4]) # (1, 2, 3, 4)
From String¶
# O(n) - where n = string length
t = tuple("hello") # ('h', 'e', 'l', 'l', 'o')
t = tuple("abc") # ('a', 'b', 'c')
From Other Iterables¶
# O(n) - where n = iterable length
t = tuple((1, 2, 3)) # (1, 2, 3)
t = tuple({1, 2, 3}) # Order depends on set iteration
t = tuple(range(5)) # (0, 1, 2, 3, 4)
t = tuple(map(str, [1, 2])) # ('1', '2')
From Dictionary¶
# O(n) - uses dict keys
d = {"a": 1, "b": 2, "c": 3}
t = tuple(d) # ('a', 'b', 'c') - keys in insertion order
Complexity Details¶
Iterating and Copying¶
# O(n) - must iterate to create tuple
original = [1, 2, 3, 4, 5]
t = tuple(original) # O(5) - creates (1, 2, 3, 4, 5)
# Each element is accessed once
for item in iterable: # O(n)
add to tuple
From String¶
# O(n) - linear in string length
short = tuple("ab") # O(2) - ('a', 'b')
long = tuple("a" * 1000) # O(1000)
# Each character becomes one tuple item
From Range¶
# O(n) - must materialize all values
small = tuple(range(5)) # O(5) - (0, 1, 2, 3, 4)
large = tuple(range(10**6)) # O(10^6) - slow!
# range() is lazy, tuple() forces evaluation
Common Patterns¶
Convert Iterable to Tuple¶
# O(n) - general conversion
def process_items(items):
items_tuple = tuple(items) # O(n) - ensure immutable
# Now can hash, use in set, etc.
return items_tuple[0] # O(1)
# Useful for generators
gen = (x for x in range(5))
t = tuple(gen) # (0, 1, 2, 3, 4)
Create Tuple from List¶
# O(n) - copy to immutable
lst = [1, 2, 3]
t = tuple(lst) # (1, 2, 3)
# Safe - can't be modified
# lst.append(4) # OK - modifies list
# t.append(4) # AttributeError - tuple immutable
Unpacking Tuple¶
# O(1) - unpack is fast
t = (1, 2, 3)
# Unpacking
a, b, c = t # O(1) - constant time
x, *rest = t # O(1) - unpacking
# Useful with tuple()
items = tuple(range(3))
x, y, z = items # O(1)
Performance Patterns¶
Tuple Literal vs tuple()¶
# Both O(n) but different syntax
# Literal - faster, no function call
t1 = (1, 2, 3, 4, 5) # O(n)
# Function - more flexible
t2 = tuple([1, 2, 3, 4, 5]) # O(n)
t3 = tuple(range(5)) # O(n)
# Literal usually preferred for constants
Tuple as Cache Key¶
# O(n) - create tuple for caching
from functools import lru_cache
@lru_cache(maxsize=128)
def compute(values):
return sum(values)
# Convert to tuple before caching (hashable)
result = compute(tuple([1, 2, 3])) # O(3)
result = compute((1, 2, 3)) # O(1) - cache hit
Batch Conversion¶
# O(n * m) - n items, m = avg iterable length
data = [[1, 2], [3, 4], [5, 6]]
tuples = [tuple(item) for item in data] # O(n*m)
# Using map
tuples = list(map(tuple, data)) # O(n*m)
Edge Cases¶
Empty Tuple¶
# O(1)
t = tuple() # ()
t = tuple([]) # ()
t = tuple("") # ()
Single Item¶
# O(1) - must use trailing comma for single item
t = (1,) # (1,) - tuple
x = (1) # 1 - just int!
# tuple() handles this
t = tuple([1]) # (1,)
Very Large Tuple¶
# O(n) - allocates large tuple
huge_range = range(10**9)
t = tuple(huge_range) # O(10^9) - very slow and memory-intensive!
# Better - keep as range
for item in huge_range: # No allocation
process(item)
Nested Tuples¶
# O(n) - contains other tuples
inner1 = (1, 2)
inner2 = (3, 4)
outer = (inner1, inner2) # ((1, 2), (3, 4))
# Shallow copy
copied = tuple(outer) # Still contains references to inner tuples
Immutability¶
# O(1) - immutable, cannot modify
t = (1, 2, 3)
# Cannot change
# t[0] = 10 # TypeError - tuple doesn't support item assignment
# t.append(4) # AttributeError - no append method
# But contents can be mutable
t_with_list = ([1, 2], [3, 4])
t_with_list[0][0] = 999 # OK - modifies inner list
Tuple Unpacking¶
# O(1) - unpacking is very fast
t = (1, 2, 3)
# Single assignment
a, b, c = t # O(1)
# With unpacking operator
x, *rest = t # O(1)
# Extended unpacking
a, *middle, z = t # O(1)
Best Practices¶
✅ Do:
- Use tuple() to convert iterables to immutable
- Use tuples for hashable sequences (dict keys, set members)
- Use tuple unpacking for readability
- Use tuple literal (1, 2, 3) for constants
❌ Avoid:
- Using tuple() when you need to modify items (use list)
- Creating tuples from very large iterables (memory)
- Forgetting trailing comma for single-item tuple: (1) vs (1,)
- Assuming tuple is always faster (usually similar to list)
Related Functions¶
- list() - Convert to list
- set() - Convert to set
- dict() - Convert to dictionary
- zip() - Create tuples from iterables
Version Notes¶
- Python 2.x: tuple() available, creates tuples
- Python 3.x: Same behavior
- All versions: Immutable, hashable sequences