Tuple
A tuple is an immutable list. A tuple cannot be modified in any way once it has been created.
Why are tuples needed?
- In the case of protecting any data from changes (intentional or accidental).
- Tuples take up less memory space and are faster than lists.
- To return multiple values from a function.
- Tuples can be used as dictionary keys (more on that later).
Creating tuples
1 way: like a list, but with parentheses
1
2
3
|
a = (1,2,3,4,5)
print(type(a)) # class 'tuple'
print(a) # (1, 2, 3, 4, 5)
|
2 way: using tuple() function
1
2
3
|
b = tuple((1,2,3,4,5))
print(type(b)) # <class 'tuple'>
print(b) # (1, 2, 3, 4, 5)
|
You have to remember!
- Tuples are an immutable list.
- The elements of a tuple can be of different types.
- You can create a tuple using the
tuple() function or by listing the elements in parentheses.
|
Tuple Features
1) You can assign values to a tuple without using parentheses/
For example, you can create a tuple like this:
1
2
3
|
a = 1,2,3,4,5
print(a[3]) # 4 An element of a tuple can be accessed in the same way as an element of a list,
# by specifying the element index in square brackets.
|
2) To declare a tuple that includes one single element, you need to use a trailing comma:
1
2
3
4
|
a = 'a'
b = 'b',
print(type(b)) # <class 'tuple'>
print(type(a)) # <class 'str'>
|
3) You can assign the values of the elements of the tuple to individual variables:
1
2
3
|
my_tuple = (1, 2, 3, 4, 5)
a, b, c, d, e = my_tuple
print(c) #3
|
Underscores _ can be used as unnecessary variables
1
2
3
|
my_tuple = (1, 2, 3)
a, _, _ = my_tuple
print(a) #1
|
Number of variables must match with the number of elements of the tuple!
4) A tuple can contain various nested elements. In this case, when referring to nested elements, you must use additional square brackets
1
2
|
my_tuple = (('a', 'b', 'c'), [1, 2], ((1, 'a' ), ('b', 'c')))
print(my_tuple[2][1]) # ('b', 'c')
|
|
Referring to elements. Nested Tuples
Tuples can contain other tuples as elements. Also, lists, strings, etc. can be used as elements of a tuple.
Accessing elements is similar to accessing elements of a list, specifying the index of the element in square brackets. Indexing starts from zero.
When referring to nested elements, additional square brackets must be used.
my_tuple = (('a', 'b', 'c'), [1, 2], ((1, 'a' ), ('b', 'c')))
print(my_tuple[2][1]) # ('b', 'c')
|
Comparing tuples
When comparing tuples:
- numbers are compared by value;
- strings in lexicographical order;
- in case of equality of elements in the same positions, the following elements are compared;
- comparison of elements will occur until the first inequality;
- when comparing, elements must be cast to the same type (you cannot compare a number and a string).
Example
1
2
3
4
5
6
7
8
9
10
eleven
12
|
A=4
B=8
C = 'a',
D = 'z',
E = (14, 'maximum', 'minimum')
F = (14, 'maximum', 'min')
K=999
print(A < B) # True
print(C < D) # True
print(E > F) # True
print(K < F) # False
print(C < K) # TypeError: '<' not supported
# between instances of 'str' and 'int'
|
|
Slices
Slicing can be performed similarly to lists.
It should be remembered that by taking an element by index or by slice ( slice ), we do not change the tuple in any way, we simply copied part of it for further use (for example, for printing, some calculations, etc. .p.).
Slice syntax
my_tuple[start:stop:step] # start, stop and step
|
Tuple concatenation
It is possible to concatenate tuples to create a new object (concatenation, similar to strings).
1
2
3
4
|
x = (1,2,3,4)
y = (5,6,7,8)
z = x + y
print(z) # (1, 2, 3, 4, 5, 6, 7, 8)
|
During the multiplication operation, the tuple is repeated several times (similar to string multiplication).
1
2
3
|
x = (1,2,3,4)
z = x*2
print(z) # (1, 2, 3, 4, 1, 2, 3, 4)
|
Removing a tuple
Tuple operations and methods are similar to list operations and methods. Except for those that change the elements of a tuple.
An element contained in a tuple cannot be added or removed due to immutability. However, the tuple itself can be deleted using the del operator.
1
2
|
student = (2007, 'Ivan', 'Ivanov', '9-A', False)
del student
|
|
Methods for working with tuples
Methods for working with tuples are similar to methods for working with lists, except for methods that change the tuple. Such methods are not available due to the immutability of the tuple. They can be applied by creating a new tuple.
Tuple sort example
You can easily sort a tuple using the sorted() function.
1
2
3
4
|
a = (5, 3, 2, 1, 4)
print(sorted(a)) # [1, 2, 3, 4, 5]
a = tuple(sorted(a))
print(a) # (1, 2, 3, 4, 5)
|
Note that methods such as append() , extend() , remove() do NOT work with tuples and pop() .
|