Python Tuple - Slicing using step parameter

To slice a tuple by skipping elements at specific index intervals use the subscript notation of the form t[i,j,j].

Tuple Updated 18 Sep 2016 1 min read

The slicing operation on a python tuple with a step option, creates a slice by skipping elements at intervals specified the Step Value.

Parameters

BeginIndex - Starting index of the tuple object
EndIndex - EndIndex+1 of the tuple object
Step Value - Interval on which indexes and the elements at these indexes need to skipped

Return Value

A tuple as specified in the slice method paramters. The tuple created is a result of shallow copying from the original tuple.

Example

python
>>> FruitBasket = ('Apple', 'Orange', 'Banana', 'Grape', 'Raspberry')

>>> MyFruitPlate=FruitBasket[0:5:1]

>>> print(MyFruitPlate)

('Apple', 'Orange', 'Banana', 'Grape', 'Raspberry')

>>> MyFruitPlate=FruitBasket[0:5:2]

>>> print(MyFruitPlate)

('Apple', 'Banana', 'Raspberry')

>>> MyFruitPlate=FruitBasket[0:5:3]

>>> print(MyFruitPlate)

('Apple', 'Grape')

>>> MyFruitPlate=FruitBasket[0:5:4]

>>> print(MyFruitPlate)

('Apple', 'Raspberry')

>>> MyFruitPlate=FruitBasket[0:5:5]

>>> print(MyFruitPlate)

('Apple',)

>>> MyFruitPlate=FruitBasket[0:5:6]

>>> print(MyFruitPlate)

('Apple',)

>>> MyFruitPlate=FruitBasket[0:5:7]

>>> print(MyFruitPlate)

('Apple',)