DEV Community

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

Posted on • Edited on

Tuple in Python (1)

Buy Me a Coffee

*Memos:

  • My post explains the tuple with indexing and the useful functions.
  • My post explains the tuple with slicing and copy.
  • My post explains a list and the list with indexing.
  • My post explains a set and copy.
  • My post explains a dictionary, the dictionary with keying and copy.
  • My post explains variable assignment.
  • My post explains shallow copy and deep copy.

A tuple:

  • is an ordered collection to use non-huge data not to get MemoryError.
  • allows duplicated elements (even with different types).
  • is immutable so it cannot be changed.
  • can have any mixed types of elements.
  • can be enlarged with * and a number.
  • can be created by tuple() with or without a list, tuple, set, dictionary, iterator, string or range() or by a tuple comprehension.
  • can be used with len() to get the length.
  • can be accessed but cannot be changed by indexing or slicing.
  • cannot be copied to always refer to the same tuple.

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


You can create a tuple as shown below:

v = () # Empty 1D tuple
v = tuple() # Empty 1D tuple
v = ('a',) # 1D tuple
v = 'a', # 1D tuple
v = ('a') # It's `str` but not tuple.
v = 'a' # It's `str` but not tuple.
v = ('a'), # 1D tuple
v = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') # 1D tuple
v = 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' # 1D tuple
v = ('a', 'b', 'c', 'a', 'b', 'c') # 1D tuple
v = (('a',),) # 2D tuple
v = ((('a'),)), # 2D tuple
v = ('a', 'b', 'c', 'd', ('e', 'f', 'g', 'h')) # 2D tuple
v = (('a', 'b', 'c', 'd'), ('e', 'f', 'g', 'h')) # 2D tuple
v = ((('a',),),) # 3D tuple
v = ((((('a'),)),)), # 3D tuple
v = (('a', 'b', 'c', 'd'), (('e', 'f'), ('g', 'h'))) # 3D tuple
v = ((('a', 'b'), ('c', 'd')), (('e', 'f')), ('g', 'h'))) # 3D tuple
v = (1, 1.0, 1.0+0.0j, True)
v = ('a', 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, {'a':'A'})
# No error
Enter fullscreen mode Exit fullscreen mode

A tuple is an ordered collection as shown below:

v = ('a', 'b', 'c', 'd', 'e')

print(v) # ('a', 'b', 'c', 'd', 'e')
Enter fullscreen mode Exit fullscreen mode

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

v = ('a', 'b', 'c', 'a', 'b', 'c')

print(v) # ('a', 'b', 'c', 'a', 'b', 'c')
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 mixed types of elements as shown below:

v = ('a', 2, 2.3, 2.3+4.5j, True, [2, 3], (2, 3), {2, 3}, {'a':'A'})

print(v)
# ('a', 2, 2.3, (2.3+4.5j), True, [2, 3], (2, 3), {2, 3}, {'a': 'A'})
Enter fullscreen mode Exit fullscreen mode

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

1D list:
v1 = ('a', 'b', 'c') * 3
v2 = (0,) * 3

print(v1) # ('a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c')
print(v2) # (0, 0, 0)
Enter fullscreen mode Exit fullscreen mode
2D list:
v1 = (('a', 'b', 'c'),) * 3
v2 = ((0,),) * 3

print(v1) # (('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c'))
print(v2) # ((0,), (0,), (0,))
Enter fullscreen mode Exit fullscreen mode
3D list:
v1 = ((('a', 'b', 'c'),),) * 3
v2 = (((0,),),) * 3

print(v1) # ((('a', 'b', 'c'),), (('a', 'b', 'c'),), (('a', 'b', 'c'),))
print(v2) # (((0,),), ((0,),), ((0,),))
Enter fullscreen mode Exit fullscreen mode

tuple() can create a tuple with or without a list, tuple, set, dictionary, iterator, string or range() as shown below:

*Memos:

  • The 1st argument is iterable(Optional-Type:iterable).
  • Don't use iterable=.
v = tuple() # Empty tuple

print() # ()
Enter fullscreen mode Exit fullscreen mode
v = ['a', 'b', 'c', 'd', 'e'] # List

print(tuple(v)) # ('a', 'b', 'c', 'd', 'e')
Enter fullscreen mode Exit fullscreen mode
v = ('a', 'b', 'c', 'd', 'e') # Tuple

print(tuple(v)) # ('a', 'b', 'c', 'd', 'e')
Enter fullscreen mode Exit fullscreen mode
v = {'a', 'b', 'c', 'd', 'e'} # Set

print(tuple(v)) # ('c', 'a', 'd', 'b', 'e')
Enter fullscreen mode Exit fullscreen mode
v = {'name': 'John', 'age': 36, 'gender': 'Male'} # Dictionary

print(tuple(v))
print(tuple(v.keys()))
# ('name', 'age', 'gender')

print(tuple(v.values()))
# ('John', 36, 'Male')

print(tuple(v.items()))
# (('name', 'John'), ('age', 36), ('gender', 'Male'))
Enter fullscreen mode Exit fullscreen mode
v = iter(['a', 'b', 'c', 'd', 'e']) # Iterator

print(tuple(v)) # ('a', 'b', 'c', 'd', 'e')
Enter fullscreen mode Exit fullscreen mode
v = 'Hello' # String

print(tuple(v)) # ('H', 'e', 'l', 'l', 'o')
Enter fullscreen mode Exit fullscreen mode
v = range(5)

print(tuple(v)) # (0, 1, 2, 3, 4)
Enter fullscreen mode Exit fullscreen mode

A tuple comprehension can create a tuple as shown below:

v = tuple(x**2 for x in range(6))

print(v) # (0, 1, 4, 9, 16, 25)
Enter fullscreen mode Exit fullscreen mode

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

v = ('a', 'b', 'c') * 1000000000
# MemoryError
Enter fullscreen mode Exit fullscreen mode
v = range(100000000)

print(tuple(v)) # MemoryError
Enter fullscreen mode Exit fullscreen mode

Top comments (0)