# Python – Pair iteration in list

## Python | Pair iteration in list

List iteration is common in programming, but sometimes one requires to print the elements in consecutive pairs. This particular problem is quite common and having a solution to it always turns out to be handy. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using list comprehension
List comprehension can be used to print the pairs by accessing current and next element in the list and then printing the same. Care has to be taken while pairing the last element with the first one to form a cyclic pair.

 `# Python3 code to demonstrate ` `# pair iteration in list ` `# using list comprehension` `from` `itertools ``import` `compress` ` ` `# initializing list  ` `test_list ``=` `[``0``, ``1``, ``2``, ``3``, ``4``, ``5``]` ` ` `# printing original list` `print` `(``"The original list is : "` `+` `str``(test_list))` ` ` `# using list comprehension` `# to perform pair iteration in list ` `res ``=` `[((i), (i ``+` `1``) ``%` `len``(test_list)) ` `         ``for` `i ``in` `range``(``len``(test_list))]` ` ` `# printing result` `print` `(``"The pair list is : "` `+` `str``(res))`

Output:

```The original list is : [0, 1, 2, 3, 4, 5]
The pair list is : [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 0)]
```

Method #2 : Using `zip()` + list slicing

`zip `function can be used to extract pairs over the list and slicing can be used to successively pair the current element with the next one for the efficient pairing.

 `# Python3 code to demonstrate ` `# pair iteration in list ` `# using zip() + list slicing ` `from` `itertools ``import` `compress` ` ` `# initializing list  ` `test_list ``=` `[``0``, ``1``, ``2``, ``3``, ``4``, ``5``]` ` ` `# printing original list` `print` `(``"The original list is : "` `+` `str``(test_list))` ` ` `# using zip() + list slicing ` `# to perform pair iteration in list ` `res ``=` `list``(``zip``(test_list, test_list[``1``:] ``+` `test_list[:``1``]))` ` ` `# printing result` `print` `(``"The pair list is : "` `+` `str``(res))`

Output:

```The original list is : [0, 1, 2, 3, 4, 5]
The pair list is : [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5,```

Last Updated on March 17, 2022 by admin

## numpy.cov() functionnumpy.cov() function

Python | numpy.cov() function Covariance provides the a measure of strength of correlation between two

## Randomly select n elements from list in PythonRandomly select n elements from list in Python

Randomly select n elements from list in Python In this article, we will discuss how

## Python – How to search for a string in text files?Python – How to search for a string in text files?

Python – How to search for a string in text files? In this article, we

## Using Matplotlib with Jupyter NotebookUsing Matplotlib with Jupyter Notebook

Using Matplotlib with Jupyter Notebook The Jupyter Notebook is an open-source web application that allows you to

## Difference between Pandas VS NumPyDifference between Pandas VS NumPy

Difference between Pandas VS NumPy Pandas is an open-source, BSD-licensed library written in Python Language. Pandas provide high performance, fast,

## Classifying data using Support Vector Machines(SVMs) in PythonClassifying data using Support Vector Machines(SVMs) in Python

Classifying data using Support Vector Machines(SVMs) in Python Introduction to SVMs: In machine learning, support

## Pooling Layer for Convolutional Neural Networks (CNN)Pooling Layer for Convolutional Neural Networks (CNN)

CNN | Introduction to Pooling Layer The pooling operation involves sliding a two-dimensional filter over

## break, continue and pass in Pythonbreak, continue and pass in Python

break, continue and pass in Python Using loops in Python automates and repeats the tasks