
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alternate Cycling in Python List
Lists in Python are ordered collections that store and manipulate a sequence of elements. In this article, we are going to learn alternate cycling in Python. It is used when we need to work with the alternate element, instead of processing every element in the list.
Alternate cycling is the process of accessing or iterating through list elements by skipping elements in a fixed pattern. Following is an example scenario -
ScenarioInput:[1,2,3,4,6] Output:[1,3,5] Explanation: In this case, the alternate cycling starts from the index 0 and skips the next element in the list.
Alternate Cycling Using Slicing
Slicing is used to extract a portion of a sequence (such as a list, a tuple, or a string) or other iterable objects. It provides a simple way to access a range of elements.
Syntax
Following is the syntax of Python slicing -
sequence[start:stop:step]
Where in the above syntax -
- start indicates the beginning element.
- stop indicates the element at the end.
-
step is the increment value for every iteration.
You can retrieve alternate elements from a Python List using the slice operator with 2 as the step value, as shown below -
result = lst[::2]
Example
In the following example, we are going to print every alternate element from a list starting at index 0.
lst = [1,2,3,4,5,6] result = lst[::2] print(result)
The output of the above program is as follows -
[1, 3, 5]
Alternate Cycling Using zip() Function
The Python zip() function is used to combine two or more iterables (like lists, tuples, or strings) into a single iterable. It returns an iterator of tuples, where the first item of each given iterable is paired together and the second item of all iterables is paired, and so on.
Using this function, we can retrieve alternate elements from multiple lists, treating them as one. To do so, we just need to pass all the lists to the zip() function and convert the resultant list of tuples into a list.
Syntax
Following is the syntax of the Python zip() function -
zip(iterable1, iterable2, ...)
Example
Consider the following example, where we are going to create a new list by cycling alternate elements from two lists.
x1 = [12,14,16] x2 = [11,13,15] result = [] for a, b in zip(x1, x2): result.append(a) result.append(b) print(result)
Following is the output of the above program -
[12, 11, 14, 13, 16, 15]
Alternate Cycling Using Loops
Loops are control flow statements that are used to execute a block of code repeatedly. In Python, we can also iterate through a range of elements (of any iterable) using the range() function with a for loop. This function accepts 3 parameters: start, stop, and step (similar to the slice operator).
To perform alternate cycling on a list, instead of going through its every element, loop through the list using the range() function with 2 as the step value.
Example
In the following example, we are going to extract the alternate elements from the list by skipping every second element.
lst = [2,1,4,3,6,5] result = [] for i in range(0, len(lst), 2): result.append(lst[i]) print(result)
If we run the above program, it will generate the following output -
[2, 4, 6]