Tuple
Immutable, ordered series, traditionally containing different objects
Advantages: Immutable and ordered. Relatively efficient memory usage (more than lists).
Disadvantages: Searching is O(n). Hard to understand for many Python newcomers.
Creating
t = ('a', 1, [1,2,3]) # () and comma indicate tuple
t = ('a',) # single-element tuple requires ,!
Convert from another type
tuple([1,2,3]) # (1,2,3)
Iterating over the elements
t = ('a', 'b', 'c')
for item in t:
print(item)
Iterating over the sorted elements
t = ('d', 'a', 'c', 'b')
for item in sorted(t):
print(item)
Last updated
Was this helpful?