Open in App
Not now

# Python | Alternate range slicing in list

• Last Updated : 07 Jan, 2023

List slicing is quite common utility in Python, one can easily slice certain elements from a list, but sometimes, we need to perform that task in non-contiguous manner and slice alternate ranges. Let’s discuss how this particular problem can be solved.

Method #1 : Using list comprehension List comprehension can be used to perform this particular task with ease as it can be used to run a loop and only filter the elements that leave a remainder more than half of target slice size multiplied by 2. By this way we can extract the sliced numbers in range alternatively.

## Python3

 `# Python3 code to demonstrate` `# alternate range slicing ` `# using list comprehension`   `# initializing list ` `test_list ``=` `[``2``, ``4``, ``6``, ``8``, ``9``, ``10``, ``12``, ``16``, ``18``, ``20``, ``7``, ``30``]`   `# printing original list` `print``("The original ``list` `: " ``+` `str``(test_list))`   `# Select range size ` `N ``=` `3`   `# using list comprehension` `# alternate range slicing` `res ``=` `[test_list[i] ``for` `i ``in` `range``(``len``(test_list)) ` `                              ``if` `i ``%` `(N ``*` `2``) >``=` `N]`   `# print result` `print``("The alternate ``range` `sliced ``list` `: " ``+` `str``(res))`

Output :

```The original list : [2, 4, 6, 8, 9, 10, 12, 16, 18, 20, 7, 30]
The alternate range sliced list : [8, 9, 10, 20, 7, 30]```

Method #2 : Using enumerate() + list comprehension The list comprehension can also be combined with the enumerate function to perform this task. The advantage of using enumerate is that we can track of index along with the value and it’s more efficient and has lesser run-time than the above function.

## Python3

 `# Python3 code to demonstrate` `# alternate range slicing ` `# using list comprehension + enumerate()`   `# initializing list ` `test_list ``=` `[``2``, ``4``, ``6``, ``8``, ``9``, ``10``, ``12``, ``16``, ``18``, ``20``, ``7``, ``30``]`   `# printing original list` `print``("The original ``list` `: " ``+` `str``(test_list))`   `# Select range size ` `N ``=` `3`   `# using list comprehension + enumerate()` `# alternate range slicing` `res ``=` `[val ``for` `i, val ``in` `enumerate``(test_list)` `                         ``if` `i ``%` `(N ``*` `2``) >``=` `N]`   `# print result` `print``("The alternate ``range` `sliced ``list` `: " ``+` `str``(res))`

Output :

```The original list : [2, 4, 6, 8, 9, 10, 12, 16, 18, 20, 7, 30]
The alternate range sliced list : [8, 9, 10, 20, 7, 30]```

Method #3 : Using itertools +for loop

In this code, a list test_list is initialized with a set of values, and a variable N is defined as the range size for the alternate slicing.

Then, a loop is used to iterate over the elements of the test_list using the range() function. For each iteration, the index i is checked to see if it leaves a remainder of more than half of N when divided by N multiplied by 2. If the condition is true, the islice() function is used to slice the list and return only the element at the current index i. The result of the islice() function is then appended to the res list using the extend() method.

Finally, the res list is printed to the console.

## Python3

 `from` `itertools ``import` `islice`   `# initializing list ` `test_list ``=` `[``2``, ``4``, ``6``, ``8``, ``9``, ``10``, ``12``, ``16``, ``18``, ``20``, ``7``, ``30``]`   `# Select range size ` `N ``=` `3`   `# using islice()` `# alternate range slicing` `res ``=` `[]` `for` `i ``in` `range``(``len``(test_list)):` `    ``if` `i ``%` `(N ``*` `2``) >``=` `N:` `        ``res.extend(islice(test_list, i, i``+``1``))`   `# print result` `print``(``"The alternate range sliced list:"``, res)` `#This code is contributed by Edula Vinay Kumar Reddy`

Output

`The alternate range sliced list: [8, 9, 10, 20, 7, 30]`

The time complexity of this code is O(n), where n is the length of the test_list. This is because the loop iterates over the elements of the list once. The space complexity of this code is also O(n), because the res list is created and grows by one element for each iteration of the loop.

My Personal Notes arrow_drop_up
Related Articles