ACCESSING MIXED TUPLE IN PYTHON
INTRODUCTION
Mixed tuple will contain variable from different data types like integer, string, floating, boolean and even complex data types like list, tuple, dictionary, sets etc. as a field.
ORDER OF PYTHON TOUPLE
Order in Python list is very important. Because tuple fields are accessed in the order they appear in the tuple.
# Mixed tuple example 1
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', 10.6, True)
print("\nElement of tuple ->", mixed_tuple)
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', 10.6, True)
TOUPLE INDEX
Indexes are used to access the individual field in the tuple. Index in Python tuple starts with 0.
# Mixed tuple example 2
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True)
print("\nElement of tuple ->", mixed_tuple)
print("\n1st field of tuple mixed_tuple ->", mixed_tuple[0])
print("2nd field of tuple mixed_tuple ->", mixed_tuple[1])
print("3rd field of tuple mixed_tuple ->", mixed_tuple[2])
print("4th field of tuple mixed_tuple ->", mixed_tuple[3])
print("5th field of tuple mixed_tuple ->", mixed_tuple[4])
print("6th field of tuple mixed_tuple ->", mixed_tuple[5])
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True)
1st field of tuple mixed_tuple -> 400
2nd field of tuple mixed_tuple -> 100
3rd field of tuple mixed_tuple -> Python
4th field of tuple mixed_tuple -> 5.8
5th field of tuple mixed_tuple -> C++
6th field of tuple mixed_tuple -> True
ACCESSING TUPLE INDEX IN REVERSE ORDER
Python allows negative values as index which in turn helps us to access the tuple fields in reverse order.
When we are accessing the tuple fields from reverse order then index position 0 will not be used as we are using negative index.
# Mixed tuple example 3
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True)
print("\nElement of tuple ->", mixed_tuple)
print("\n1st field of tuple mixed_tuple ->", mixed_tuple[-6])
print("2nd field of tuple mixed_tuple ->", mixed_tuple[-5])
print("3rd field of tuple mixed_tuple ->", mixed_tuple[-4])
print("4th field of tuple mixed_tuple ->", mixed_tuple[-3])
print("5th field of tuple mixed_tuple ->", mixed_tuple[-2])
print("6th field of tuple mixed_tuple ->", mixed_tuple[-1])
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True)
1st field of tuple mixed_tuple -> 400
2nd field of tuple mixed_tuple -> 100
3rd field of tuple mixed_tuple -> Python
4th field of tuple mixed_tuple -> 5.8
5th field of tuple mixed_tuple -> C++
6th field of tuple mixed_tuple -> True
In the above example, we can see that by using negative index values we have printed the tuple in normal order. In below example we will see that if we start with index value -1 then the entire tuple will be printed in reverse order.
# Nested tuple example 4
nest_tuple = ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True)
print("\nFields in the tuple -> ", nest_tuple)
print("\n1st field of tuple nest_tuple from the end ->", nest_tuple[-1])
print("2nd field of tuple nest_tuple from the end ->", nest_tuple[-2])
print("3rd field of tuple nest_tuple from the end ->", nest_tuple[-3])
print("4th field of tuple nest_tuple from the end ->", nest_tuple[-4])
print("5th field of tuple nest_tuple from the end ->", nest_tuple[-5])
print("6th field of tuple nest_tuple from the end ->", nest_tuple[-6])
ACCESSING MIXED TUPLE IN PYTHON : Output
Fields in the tuple -> ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True)
1st field of tuple nest_tuple from the end -> True
2nd field of tuple nest_tuple from the end -> 5.8
3rd field of tuple nest_tuple from the end -> ('Java', 'PHP', 'XML')
4th field of tuple nest_tuple from the end -> Python
5th field of tuple nest_tuple from the end -> [100, 150, 500]
6th field of tuple nest_tuple from the end -> (400, 200, 600)
If we try to access a field of tuple which does not exists, then Python will raise an error -> “tuple index out of range“
# Mixed tuple example 5
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True)
print("\nElement of tuple ->", mixed_tuple)
print("\n1st field of tuple mixed_tuple ->", mixed_tuple[0])
print("2nd field of tuple mixed_tuple ->", mixed_tuple[1])
print("3rd field of tuple mixed_tuple ->", mixed_tuple[2])
print("4th field of tuple mixed_tuple ->", mixed_tuple[3])
print("5th field of tuple mixed_tuple ->", mixed_tuple[4])
print("6th field of tuple mixed_tuple ->", mixed_tuple[5])
print("7th field of tuple mixed_tuple ->", mixed_tuple[6])
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True)
1st field of tuple mixed_tuple -> 400
2nd field of tuple mixed_tuple -> 100
3rd field of tuple mixed_tuple -> Python
4th field of tuple mixed_tuple -> 5.8
5th field of tuple mixed_tuple -> C++
6th field of tuple mixed_tuple -> True
Traceback (most recent call last):
File "d:\PYTHON\PROGRAM\01_hello.py", line 17, in <module>
print("7th field of tuple mixed_tuple ->", mixed_tuple[6])
~~~~~~~~~~~^^^
IndexError: tuple index out of range
In the above example, we have tried to access the 7th field of the tuple which doesn’t exists. So python has raised the error.
len() FUNCTION
len() function helps us to count the mixed fields present in the tuple including duplicates.
# Mixed tuple example 6
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
print("\nElement of tuple ->", mixed_tuple)
print("\nNumber of element present in the tuple mixed_tuple ->", len(mixed_tuple))
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
Number of element present in the tuple mixed_tuple -> 8
count() FUNCTION
count() function returns the number of occurrences of a field in a tuple. If any field is not present in the tuple then count() function will return 0.
# Mixed tuple example 7
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
print("\nElement of tuple ->", mixed_tuple)
print("\nNumber of occurrence of Python in the tuple str_tuple ->", mixed_tuple.count('Python'))
print("\nNumber of occurrence of True in the tuple str_tuple ->", mixed_tuple.count(True))
print("\nNumber of occurrence of 700 in the tuple str_tuple ->", mixed_tuple.count(700))
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
Number of occurrence of Python in the tuple str_tuple -> 2
Number of occurrence of True in the tuple str_tuple -> 2
Number of occurrence of 700 in the tuple str_tuple -> 0
set() FUNCTION
set() function removes the duplicate fields from the tuple.
# Mixed tuple example 8
mixed_tuple = (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
print("\nElement of tuple ->", mixed_tuple)
print("\nElement of tuple after removing duplicates ->", set(mixed_tuple))
ACCESSING MIXED TUPLE IN PYTHON : Output
Element of tuple -> (400, 100, 'Python', 5.8, 'C++', True, 'Python', True)
Element of tuple after removing duplicates -> {True, 100, 5.8, 400, 'Python', 'C++'}
In the above example, we can see that the original tuple contains “Python“, True twice. But after using the set() function, the duplicates have been removed. The revised tuple contains only one field for “Python“, True. Also the output is enclosed with curly brackets which indicates the output is a set.
RELATED TOPICS:
- INTRODUCTION TO PYTHON TUPLE
- ACCESSING NUMBER TUPLE IN PYTHON
- ACCESSING MIXED TUPLE IN PYTHON
- ACCESSING NESTED TUPLE IN PYTHON
- PYTHON TOUPLE SLICING OPERATION
- STEP SIZE IN PYTHON TUPLE SLICING
- IMMUTABILITY OF TUPLE IN PYTHON
- ZIP FUNCTION IN PYTHON TUPLE
- PYTHON TUPLE SORTING OPERATIONS
- PYTHON TUPLE BUILT-IN FUNCTIONS
- REOVING TUPLE DUPLICATES USING SET
- PYTHON TUPLE SHALLOW COPY
- PYTHON TUPLE DEEP COPY
Pingback: PYTHON TOUPLE SLICING OPERATION - Sayantan's Blog On Python Programming