DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Tuple in Python (1)

Buy Me a Coffee

*Memo:

  • My post explains tuple functions and the tuple with indexing.
  • My post explains the tuple with slicing and copy.
  • My post explains a list and tuple comprehension.
  • My post explains the shallow copy and deep copy of a tuple.
  • My post explains a list and the list with indexing.
  • My post explains a set and the set with copy.
  • My post explains a dictionary (1).
  • My post explains an iterator (1).
  • My post explains a string.
  • My post explains a bytes.
  • My post explains a bytearray.

A tuple:

  • is the ordered collection of zero or more elements whose type is tuple:
    • Ordered means that the order of the elements in a tuple is kept so it guarantees that the order is always the same unless someone or something changes it.
  • shouldn't be huge not to get MemoryError.
  • allows duplicated elements (even with different types).
  • is immutable so it cannot be changed.
  • can have any types of elements.
  • can be iterated with a for statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • is False if it's empty.
  • can be checked if a specific element is or isn't in it with in keyword or not and in keyword respectively.
  • can be checked if it is or isn't referred to by two variables with is keyword or not and is keyword respectively.
  • can be enlarged with * and a number.
  • can be created by (), ,, tuple() with or without an iterable or tuple comprehension:
    • For tuple(), the words type conversion are also suitable in addition to the word creation.
  • can be used with len() to get the length.
  • can be read but cannot be changed by indexing or slicing.
  • can be continuously used through multiple variables.
  • cannot be copied to always refer to the same tuple.

A tuple is for non-huge data otherwise it gets MemoryError.


() or , can create a tuple as shown below:

v = () # Empty 1D tuple v = (0,) # 1D tuple v = 0, # 1D tuple v = (0) # int not tuple v = 0 # int not tuple v = (0), # 1D tuple v = (0, 1, 2, 3, 4) # 1D tuple v = 0, 1, 2, 3, 4 # 1D tuple v = (0, 1, 2, 0, 1, 2) # 1D tuple v = ((0,),) # 2D tuple v = (((0),)), # 2D tuple v = (0, 1, 2, 3, (4, 5, 6, 7)) # 2D tuple v = ((0, 1, 2, 3), (4, 5, 6, 7)) # 2D tuple v = (((0,),),) # 3D tuple v = (((((0),)),)), # 3D tuple v = ((0, 1, 2, 3), ((4, 5), (6, 7))) # 3D tuple v = (((0, 1), (2, 3)), ((4, 5), (6, 7))) # 3D tuple # No error  v = (0, 0.0, 0.0+0.0j, False) v = ('A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A':'a'}, range(2, 3), iter([2, 3])) for x in (0, 1, 2, 3, 4): pass for x in ((0, 1, 2, 3), (4, 5, 6, 7)): pass for x in (((0, 1), (2, 3)), ((4, 5), (6, 7))): pass v1, v2, v3 = (0, 1, 2) v1, *v2, v3 = (0, 1, 2, 3, 4, 5) for v1, v2, v3 in ((0, 1, 2), (3, 4, 5)): pass for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)): pass print((*(0, 1, *(2,)), *(3, 4))) print(*(0, 1, *(2,)), *(3, 4)) v = (0, 1, 2) * 3 v = ((0, 1, 2)) * 3 v = (((0, 1, 2))) * 3 v = tuple(x**2 for x in (0, 1, 2, 3, 4, 5, 6, 7)) v = tuple(tuple(y**2 for y in x) for x in ((0, 1, 2, 3), (4, 5, 6, 7))) v = tuple(tuple(tuple(z**2 for z in y) for y in x) for x in (((0, 1), (2, 3)), ((4, 5), (6, 7)))) # No error  print(**(0, 1, 2, 3, 4)) v = (0, 1, 2) * 1000000000 # Error 
Enter fullscreen mode Exit fullscreen mode

A tuple is the ordered collection of zero or more elements whose type is tuple as shown below:

v = (0, 1, 2, 3, 4) v = 0, 1, 2, 3, 4 print(v) # (0, 1, 2, 3, 4)  print(type(v)) # <class 'tuple'> 
Enter fullscreen mode Exit fullscreen mode
v = (0,) v = 0, v = ((0,)) print(v) # (0,) 
Enter fullscreen mode Exit fullscreen mode
v = () # Empty tuple  print(v) # () 
Enter fullscreen mode Exit fullscreen mode

A tuple allows duplicated elements (even with different types) as shown below:

v = (0, 1, 2, 0, 1, 2) print(v) # (0, 1, 2, 0, 1, 2) 
Enter fullscreen mode Exit fullscreen mode
v = (0, 0.0, 0.0+0.0j, False) print(v) # (0, 0.0, 0j, False) 
Enter fullscreen mode Exit fullscreen mode
v = (1, 1.0, 1.0+0.0j, True) print(v) # (1, 1.0, (1+0j), True) 
Enter fullscreen mode Exit fullscreen mode

A tuple can have any types of elements as shown below:

v = ('A', b'A', bytearray(b'A'), 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A':'a'}, range(2, 3), iter([2, 3])) print(v) # ('A', b'A', bytearray(b'A'), 2, 2.3, (2.3+4.5j), True, # [2, 3], (2, 3), {2, 3}, frozenset({2, 3}), {'A': 'a'}, # range(2, 3), <list_iterator object at 0x000001F3B99BF250>) 
Enter fullscreen mode Exit fullscreen mode

A tuple can be iterated with a for statement as shown below:

<1D tuple>:

for x in (0, 1, 2, 3, 4): print(x) # 0 # 1 # 2 # 3 # 4 
Enter fullscreen mode Exit fullscreen mode

<2D tuple>:

for x in ((0, 1, 2, 3), (4, 5, 6, 7)): for y in x: print(y) # 0 # 1 # 2 # 3 # 4 # 5 # 6 # 7 
Enter fullscreen mode Exit fullscreen mode

<3D tuple>:

for x in (((0, 1), (2, 3)), ((4, 5), (6, 7))): for y in x: for z in y: print(z) # 0 # 1 # 2 # 3 # 4 # 5 # 6 # 7 
Enter fullscreen mode Exit fullscreen mode

A tuple can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = (0, 1, 2) print(v1, v2, v3) # 0 1 2 
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = (0, 1, 2, 3, 4, 5) print(v1, v2, v3) # 0 [1, 2, 3, 4] 5 print(v1, *v2, v3) # 0 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in ((0, 1, 2), (3, 4, 5)): print(v1, v2, v3) # 0 1 2 # 3 4 5 
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 10, 11)): print(v1, v2, v3) print(v1, *v2, v3) # 0 [1, 2, 3, 4] 5 # 0 1 2 3 4 5 # 6 [7, 8, 9, 10] 11 # 6 7 8 9 10 11 
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'): print(p1, p2, p3, p4, p5, p6) func() # a b c d e f  func(*(0, 1, 2, 3), *(4, 5)) # 0 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args): print(p1, p2, args) print(p1, p2, *args) print(p1, p2, ['A', 'B', *args, 'C', 'D']) func() # a b () # a b Nothing # a b ['A', 'B', 'C', 'D']  func(*(0, 1, 2, 3), *(4, 5)) # 0 1 (2, 3, 4, 5) # 0 1 2 3 4 5 # 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D'] 
Enter fullscreen mode Exit fullscreen mode
print((*(0, 1, *(2,)), *(3, 4))) # (0, 1, 2, 3, 4) 
Enter fullscreen mode Exit fullscreen mode
print(*(0, 1, *(2,)), *(3, 4)) # 0 1 2 3 4 
Enter fullscreen mode Exit fullscreen mode
print(**(0, 1, 2, 3, 4)) # TypeError: print() argument after ** must be a mapping, not tuple 
Enter fullscreen mode Exit fullscreen mode

An empty tuple is False as shown below:

print(bool(())) # Empty tuple # False  print(bool((0,))) # tuple print(bool(((),))) # tuple(Empty tuple) # True 
Enter fullscreen mode Exit fullscreen mode

A tuple can be checked if a specific element is or isn't in it with in keyword or not and in keyword respectively as shown below:

v = ('A', 'B', ('C', 'D')) print('B' in v) # True  print(('C', 'D') in v) # True  print('b' in v) # False 
Enter fullscreen mode Exit fullscreen mode
v = ('A', 'B', ('C', 'D')) print('B' not in v) # False  print(('C', 'D') not in v) # False  print('b' not in v) # True 
Enter fullscreen mode Exit fullscreen mode

A tuple can be enlarged with * and a number as shown below:

<1D tuple>:

v = (0, 1, 2) * 3 print(v) # (0, 1, 2, 0, 1, 2, 0, 1, 2) 
Enter fullscreen mode Exit fullscreen mode
v = ('A', 'B', 'C') * 3 print(v) # ('A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C') 
Enter fullscreen mode Exit fullscreen mode
v = () * 3 print(v) # () 
Enter fullscreen mode Exit fullscreen mode

<2D tuple>:

v = ((0, 1, 2),) * 3 print(v) # ((0, 1, 2), (0, 1, 2), (0, 1, 2)) 
Enter fullscreen mode Exit fullscreen mode
v = (('A', 'B', 'C'),) * 3 print(v) # (('A', 'B', 'C'), ('A', 'B', 'C'), ('A', 'B', 'C')) 
Enter fullscreen mode Exit fullscreen mode
v = ((),) * 3 print(v) # ((), (), ()) 
Enter fullscreen mode Exit fullscreen mode

<3D tuple>:

v = (((0, 1, 2),),) * 3 print(v) # (((0, 1, 2),), ((0, 1, 2),), ((0, 1, 2),)) 
Enter fullscreen mode Exit fullscreen mode
v = ((('A', 'B', 'C'),),) * 3 print(v) # ((('A', 'B', 'C'),), (('A', 'B', 'C'),), (('A', 'B', 'C'),)) 
Enter fullscreen mode Exit fullscreen mode
v = (((),),) * 3 print(v) # (((),), ((),), ((),)) 
Enter fullscreen mode Exit fullscreen mode

tuple() can create a tuple with or without an iterable as shown below:

*Memo:

  • The 1st argument is iterable(Optional-Default:()-Type:Iterable):
    • Don't use iterable=.
# Empty tuple print(tuple()) print(tuple(())) # ()  print(tuple([0, 1, 2, 3, 4])) # list print(tuple((0, 1, 2, 3, 4))) # tuple print(tuple({0, 1, 2, 3, 4})) # set print(tuple(frozenset({0, 1, 2, 3, 4}) )) # frozenset print(tuple(iter([0, 1, 2, 3, 4]))) # iterator print(tuple(range(5))) # range # (0, 1, 2, 3, 4)  print(tuple({'name': 'John', 'age': 36})) # dict print(tuple({'name': 'John', 'age': 36}.keys())) # dict.keys() # ('name', 'age')  print(tuple({'name': 'John', 'age': 36}.values())) # dict.values() # ('John', 36)  print(tuple({'name': 'John', 'age': 36}.items())) # dict.items() # (('name', 'John'), ('age', 36))  print(tuple('Hello')) # str # ('H', 'e', 'l', 'l', 'o')  print(tuple(b'Hello')) # bytes print(tuple(bytearray(b'Hello'))) # bytearray # (72, 101, 108, 108, 111) 
Enter fullscreen mode Exit fullscreen mode

A tuple comprehension can create a tuple as shown below:

<1D tuple>:

sample = (0, 1, 2, 3, 4, 5, 6, 7) v = tuple(x**2 for x in sample) print(v) # (0, 1, 4, 9, 16, 25, 36, 49) 
Enter fullscreen mode Exit fullscreen mode

<2D tuple>:

sample = ((0, 1, 2, 3), (4, 5, 6, 7)) v = tuple(tuple(y**2 for y in x) for x in sample) print(v) # ((0, 1, 4, 9), (16, 25, 36, 49)) 
Enter fullscreen mode Exit fullscreen mode

<3D tuple>:

sample = (((0, 1), (2, 3)), ((4, 5), (6, 7))) v = tuple(tuple(tuple(z**2 for z in y) for y in x) for x in sample) print(v) # (((0, 1), (4, 9)), ((16, 25), (36, 49))) 
Enter fullscreen mode Exit fullscreen mode

Be careful, a huge tuple gets MemoryError as shown below:

v = (0, 1, 2) * 1000000000 # MemoryError 
Enter fullscreen mode Exit fullscreen mode
v = range(100000000) print(tuple(v)) # MemoryError 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)