Sayantan's Blog On Python Programming

PYTHON TOUPLE SLICING OPERATION

PYTHON TOUPLE SLICING OPERATION

INTRODUCTION

Before understanding slicing operation lets look at some tuple accessing methods by using index values.

# Tuple slicing operation example 1

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of tuple int_tuple at index 0->", int_tuple[0]) 
print("Fields of tuple int_tuple at index 2->", int_tuple[2]) 

print("\nFields of tuple int_tuple at index -2->", int_tuple[-2]) 
print("Fields of tuple int_tuple at index -4->", int_tuple[-4])

PYTHON TOUPLE SLICING OPERATION : Output

Fields of tuple int_tuple at index 0-> 400
Fields of tuple int_tuple at index 2-> 600

Fields of tuple int_tuple at index -2-> 150
Fields of tuple int_tuple at index -4-> 600

In the above example, we have used positive index as well as negative index. Negative index will be counted from the end of the tuple. In the above case, -2 will return the 2nd last element 150 and -4 will return 4th last element 600.

TUPLE SLICING OPERATION

POSITIVE SLICING OPERATION

Now, Slicing operation means accessing a range of elements in a tuple.

Syntax: list_name[start_index : end_index]

Start index is inclusive i.e. the index position of the tuple and end index is exclusive i.e. it will go to the index position end index – 1.

# Tuple slicing operation example 2

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)
int_tuple_slice = int_tuple[0:4]
print("\nFields of sliced tuple ->", int_tuple_slice)
print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
print("Fields of tuple int_tuple_slice at index 1->", int_tuple_slice[1]) 
print("Fields of tuple int_tuple_slice at index 2->", int_tuple_slice[2])
print("Fields of tuple int_tuple_slice at index 3->", int_tuple_slice[3]) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (400, 200, 600, 100)

Fields of tuple int_tuple_slice at index 0-> 400
Fields of tuple int_tuple_slice at index 1-> 200
Fields of tuple int_tuple_slice at index 2-> 600
Fields of tuple int_tuple_slice at index 3-> 100

If we want to access all the elements of a tuple then we either can put the total number of elements in the end index or we can leave the end index position as null.

# Tuple slicing operation example 4

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[0:]
print("\nFields of sliced tuple ->", int_tuple_slice)

print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
print("Fields of tuple int_tuple_slice at index 1->", int_tuple_slice[1]) 
print("Fields of tuple int_tuple_slice at index 2->", int_tuple_slice[2])
print("Fields of tuple int_tuple_slice at index 3->", int_tuple_slice[3]) 
print("Fields of tuple int_tuple_slice at index 4->", int_tuple_slice[4]) 
print("Fields of tuple int_tuple_slice at index 5->", int_tuple_slice[5]) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (400, 200, 600, 100, 150, 500)

Fields of tuple int_tuple_slice at index 0-> 400
Fields of tuple int_tuple_slice at index 1-> 200
Fields of tuple int_tuple_slice at index 2-> 600
Fields of tuple int_tuple_slice at index 3-> 100
Fields of tuple int_tuple_slice at index 4-> 150
Fields of tuple int_tuple_slice at index 5-> 500

# Tuple slicing operation example 4

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[0:]
print("\nFields of sliced tuple ->", int_tuple_slice)

print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
print("Fields of tuple int_tuple_slice at index 1->", int_tuple_slice[1]) 
print("Fields of tuple int_tuple_slice at index 2->", int_tuple_slice[2])
print("Fields of tuple int_tuple_slice at index 3->", int_tuple_slice[3]) 
print("Fields of tuple int_tuple_slice at index 4->", int_tuple_slice[4]) 
print("Fields of tuple int_tuple_slice at index 5->", int_tuple_slice[5])

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (400, 200, 600, 100, 150, 500)

Fields of tuple int_tuple_slice at index 0-> 400
Fields of tuple int_tuple_slice at index 1-> 200
Fields of tuple int_tuple_slice at index 2-> 600
Fields of tuple int_tuple_slice at index 3-> 100
Fields of tuple int_tuple_slice at index 4-> 150
Fields of tuple int_tuple_slice at index 5-> 500

If we put the end index as a number beyond the max limit of the tuple then also the entire tuple will be printed.

# Tuple slicing operation example 5

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[0:7]
print("\nFields of sliced tuple ->", int_tuple_slice)

print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
print("Fields of tuple int_tuple_slice at index 1->", int_tuple_slice[1]) 
print("Fields of tuple int_tuple_slice at index 2->", int_tuple_slice[2])
print("Fields of tuple int_tuple_slice at index 3->", int_tuple_slice[3]) 
print("Fields of tuple int_tuple_slice at index 4->", int_tuple_slice[4]) 
print("Fields of tuple int_tuple_slice at index 5->", int_tuple_slice[5]) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (400, 200, 600, 100, 150, 500)

Fields of tuple int_tuple_slice at index 0-> 400
Fields of tuple int_tuple_slice at index 1-> 200
Fields of tuple int_tuple_slice at index 2-> 600
Fields of tuple int_tuple_slice at index 3-> 100
Fields of tuple int_tuple_slice at index 4-> 150
Fields of tuple int_tuple_slice at index 5-> 500

If we put the start index as a number beyond the max limit of the tuple then python will raise error – “tuple index out of range“.

# Tuple slicing operation example 6

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[7:]
print("\nFields of sliced tuple ->", int_tuple_slice)

print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
print("Fields of tuple int_tuple_slice at index 1->", int_tuple_slice[1]) 
print("Fields of tuple int_tuple_slice at index 2->", int_tuple_slice[2])
print("Fields of tuple int_tuple_slice at index 3->", int_tuple_slice[3]) 
print("Fields of tuple int_tuple_slice at index 4->", int_tuple_slice[4]) 
print("Fields of tuple int_tuple_slice at index 5->", int_tuple_slice[5]) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> ()
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 9, in <module>
    print("\nFields of tuple int_tuple_slice at index 0->", int_tuple_slice[0]) 
                                                            ~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

If we want to extract the last element of the tuple the we can put the max index position at start index and null as end index.

# Tuple slicing operation example 7

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[5:]
print("\nFields of sliced tuple ->", int_tuple_slice)
print("Last field of tuple int_tuple_slice -> ", int_tuple_slice[0]) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (500,)
Last field of tuple int_tuple_slice ->  500

NEGATIVE SLICING OPERATION

Python allows negative slicing as well.

# Tuple slicing operation example 7

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[0:-3]
print("\nFields of sliced tuple ->", int_tuple_slice) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> (400, 200, 600)

In the above example, we have selected the start index as 0 and end index as -3. So the 3rd last element is 100. But end index is exclusive in nature so it will go up to next index i.e. -4. The 4th last element is 600. That’s why 400, 200, 600 have been show in the result.

If the range specified by slicing operation doesn’t fetch any element then python will return null.

# Tuple slicing operation example 8

int_tuple = (400, 200, 600, 100, 150, 500) 
print("\nFields of original tuple ->", int_tuple)

int_tuple_slice = int_tuple[-3:-4]
print("\nFields of sliced tuple ->", int_tuple_slice) 

PYTHON TOUPLE SLICING OPERATION : Output

Fields of original tuple -> (400, 200, 600, 100, 150, 500)

Fields of sliced tuple -> ()

In the above example we have provided start index as -3 and end index as -4. So the end index is going beyond start index. That’s why python has returned null.

PYTHON TOUPLE SLICING OPERATION : Output
PYTHON TOUPLE SLICING OPERATION : Output

RELATED TOPICS:

1 thought on “PYTHON TOUPLE SLICING OPERATION”

  1. Pingback: ACCESSING NESTED TUPLE IN PYTHON - Sayantan's Blog On Python Programming

Leave a Comment

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