Sayantan's Blog On Python Programming

ACCESSING NESTED TUPLE IN PYTHON

ACCESSING NESTED TUPLE IN PYTHON

INTRODUCTION

Nested tuple will contain complex data types like list, tuple, dictionary, sets etc. as a field. They can also contain data types like integer, string, floating, boolean etc.

ORDER OF PYTHON TOUPLE

Order in Python list is very important. Because tuple fields are accessed in the order they appear in the tuple.

# Nested tuple example 1

nest_tuple = ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'))
print("\nFields in the tuple -> ", nest_tuple)

ACCESSING NESTED TUPLE IN PYTHON : Output

Fields in the tuple ->  ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'))

TOUPLE INDEX

Indexes are used to access the individual field in the tuple. Index in Python tuple starts with 0.

# Nested tuple example 2

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 ->", nest_tuple[0])
print("2nd field of tuple nest_tuple ->", nest_tuple[1])
print("3rd field of tuple nest_tuple ->", nest_tuple[2])
print("4th field of tuple nest_tuple ->", nest_tuple[3])
print("5th field of tuple nest_tuple ->", nest_tuple[4])
print("6th field of tuple nest_tuple ->", nest_tuple[5])

ACCESSING NESTED 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 -> (400, 200, 600)
2nd field of tuple nest_tuple -> [100, 150, 500]
3rd field of tuple nest_tuple -> Python
4th field of tuple nest_tuple -> ('Java', 'PHP', 'XML')
5th field of tuple nest_tuple -> 5.8
6th field of tuple nest_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.

# Nested tuple example 3

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 ->", nest_tuple[-6])
print("2nd field of tuple nest_tuple ->", nest_tuple[-5])
print("3rd field of tuple nest_tuple ->", nest_tuple[-4])
print("4th field of tuple nest_tuple ->", nest_tuple[-3])
print("5th field of tuple nest_tuple ->", nest_tuple[-2])
print("6th field of tuple nest_tuple ->", nest_tuple[-1])

ACCESSING NESTED 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 -> (400, 200, 600)
2nd field of tuple nest_tuple -> [100, 150, 500]
3rd field of tuple nest_tuple -> Python
4th field of tuple nest_tuple -> ('Java', 'PHP', 'XML')
5th field of tuple nest_tuple -> 5.8
6th field of tuple nest_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 ->", nest_tuple[-1])
print("2nd field of tuple nest_tuple ->", nest_tuple[-2])
print("3rd field of tuple nest_tuple ->", nest_tuple[-3])
print("4th field of tuple nest_tuple ->", nest_tuple[-4])
print("5th field of tuple nest_tuple ->", nest_tuple[-5])
print("6th field of tuple nest_tuple ->", nest_tuple[-6])

ACCESSING NESTED 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 -> True
2nd field of tuple nest_tuple -> 5.8
3rd field of tuple nest_tuple -> ('Java', 'PHP', 'XML')
4th field of tuple nest_tuple -> Python
5th field of tuple nest_tuple -> [100, 150, 500]
6th field of tuple nest_tuple -> (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

# Nested tuple example 5

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 ->", nest_tuple[0])
print("2nd field of tuple nest_tuple ->", nest_tuple[1])
print("3rd field of tuple nest_tuple ->", nest_tuple[2])
print("4th field of tuple nest_tuple ->", nest_tuple[3])
print("5th field of tuple nest_tuple ->", nest_tuple[4])
print("6th field of tuple nest_tuple ->", nest_tuple[5])

print("7th field of tuple nest_tuple ->", nest_tuple[6])

ACCESSING NESTED 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 -> (400, 200, 600)
2nd field of tuple nest_tuple -> [100, 150, 500]
3rd field of tuple nest_tuple -> Python
4th field of tuple nest_tuple -> ('Java', 'PHP', 'XML')
5th field of tuple nest_tuple -> 5.8
6th field of tuple nest_tuple -> True
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 17, in <module>
    print("7th field of tuple nest_tuple ->", nest_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 nested fields present in the tuple including duplicates.

# Nested tuple example 6

nest_tuple = ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))
print("\nFields in the tuple -> ", nest_tuple)

print("\nNumber of element present in the tuple nest_tuple ->", len(nest_tuple))

ACCESSING NESTED TUPLE IN PYTHON : Output

Fields in the tuple ->  ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))

Number of element present in the tuple nest_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.

# Nested tuple example 7

nest_tuple = ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))
print("\nFields in the tuple -> ", nest_tuple)

print("\nNumber of occurrence of Python in the tuple nest_tuple ->", nest_tuple.count((400, 200, 600)))
print("\nNumber of occurrence of True in the tuple nest_tuple ->", nest_tuple.count(('Java', 'PHP', 'XML')))
print("\nNumber of occurrence of 700 in the tuple nest_tuple ->", nest_tuple.count(700))

ACCESSING NESTED TUPLE IN PYTHON : Output

Fields in the tuple ->  ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))

Number of occurrence of Python in the tuple nest_tuple -> 2

Number of occurrence of True in the tuple nest_tuple -> 2

Number of occurrence of 700 in the tuple nest_tuple -> 0

set() FUNCTION

set() function removes the duplicate fields from the tuple.

# Nested tuple example 8

nest_tuple = ((400, 200, 600), 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))
print("\nFields in the tuple -> ", nest_tuple)

print("\nElement of tuple after removing duplicates ->", set(nest_tuple))

ACCESSING NESTED TUPLE IN PYTHON : Output

Fields in the tuple ->  ((400, 200, 600), 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))

Element of tuple after removing duplicates -> {True, 5.8, (400, 200, 600), ('Java', 'PHP', 'XML'), 'Python'}

In the above example, we can see that the original tuple contains “(400, 200, 600)“, “(‘Java’, ‘PHP’, ‘XML’)” twice. But after using the set() function, the duplicates have been removed. The revised tuple contains only one field for “(400, 200, 600)“, “(‘Java’, ‘PHP’, ‘XML’)“. Also the output is enclosed with curly brackets which indicates the output is a set.

ACCESSING NESTED TUPLE IN PYTHON : Output
ACCESSING NESTED TUPLE IN PYTHON : Output

set() function doesn’t work with tuple contained any list as field.

# Nested tuple example 9

nest_tuple = ((400, 200, 600), [100, 150, 500],  'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))
print("\nFields in the tuple -> ", nest_tuple)

print("\nElement of tuple after removing duplicates ->", set(nest_tuple))

ACCESSING NESTED TUPLE IN PYTHON : Output

Fields in the tuple ->  ((400, 200, 600), [100, 150, 500], 'Python', ('Java', 'PHP', 'XML'), 5.8, True, (400, 200, 600), ('Java', 'PHP', 'XML'))Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 11, in <module>

    print("\nElement of tuple after removing duplicates ->", set(nest_tuple))
                                                             ^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *