Alternate range slicing in list (Python)



Slicing is used to extract a portion of a sequence (such as a list, a tuple, string) or other iterable objects. Python provides a flexible way to perform slicing using the following syntax.

list[start:stop:step]

Alternate Range Slicing

The alternate range slicing is used to retrieve the elements from a list by skipping the elements at a fixed interval, using the step parameter.

For example, if we want every second element from a list, we set step=2. Let's observe some examples to understand how alternate range slicing works.

Example 1

Consider the following example, where we are going to extract every second from the list.

lst = [1,2,3,4,5,6]
result = lst[::2]
print(result)

The output of the above program is as follows -

[1, 3, 5]

Example 2

In the following example, we are going to perform the alternate range slicing from a specific start and end.

lst = ["audi","benz","ciaz","dodge","etios","ferrari","g-wagon"]
result = lst[1:6:2]
print(result)

Following is the output of the above program -

['benz', 'dodge', 'ferrari']

Using reversed() with Alternate Slicing

The Python reversed() function is used to return an iterator object, which allows us to access the specified sequence in the reverse order.

Syntax

Following is the syntax of the Python reversed() function -

reversed(sequence)

To achieve the alternate range slicing in the reverse order, we are going to combine the slicing with the reversed() function. We first reverse the list and then apply the slicing[::2] to skip every second element.

Example

Following is the example where we are going to use the reversed() function to get the alternate elements in the reverse order.

lst = [1,2,3,4,5,6]
result = list(reversed(lst[::2]))
print(result)

If we run the above program, it will generate the following output -

[5, 3, 1]

Using range() and len() Functions

The Python range() function is used to return a sequence of numbers within the specified range, whereas the len() function is used to determine the length of an object (object can be a string, list, or any other iterable).

The alternate range slicing in a list using the range() and len() is achieved using the range() function with the step argument combined with the len() function to determine the total number of elements in the list.

Syntax

Following is the syntax of the Python range() function -

range(start, stop, step)
Here,
  • start: It is an optional parameter that specifies the starting position(default 0).
  • stop: This parameter represents the stop position (usually len(list)).
  • step: It specifies the required number of increments between sequences (use 2 for alternate elements).

Example

Let's look at the following example, where we are going to use the range() along with the len() function.

lst = ["activa", "BMW", "cd100", "ducati", "enfield" ,"fascino"]
result = []
for i in range(0, len(lst), 2):
    result.append(lst[i])
print(result)

Following is the output of the above program -

['activa', 'cd100', 'enfield']

Using enumerate() Function

The enumerate() function is used to access each item from the iterable object. It accepts an iterable object and returns it as an enumerate object. It also adds the index to an iterable item.

Syntax

Following is the syntax of the Python enumerate() function -

enumerate(iterable, start)

Alternate range slicing in a list is achieved by iterating through the list and selecting elements based on their index (even or odd indices to pick alternate elements).

Example

In the following example, we are going to print the alternate elements using the even indices.

lst = ["abhi","bhanu","chinna","deepu","esha","farooq"]
for index, name in enumerate(lst):
   if index % 2 == 0:
      print(f"{name}")

The output of the above program is -

abhi
chinna
esha
Updated on: 2025-07-14T14:22:54+05:30

830 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements