Python iterate by pairs

Python: Iterate over all pairs of consecutive items in a given list

Write a Python program to iterate over all pairs of consecutive items in a given list.

Sample Solution:

Python Code:

def pairwise(l1): temp = [] for i in range(len(l1) - 1): current_element, next_element = l1[i], l1[i + 1] x = (current_element, next_element) temp.append(x) return temp l1 = [1,1,2,3,3,4,4,5] print("Original lists:") print(l1) print("\nIterate over all pairs of consecutive items of the said list:") print(pairwise(l1)) 
Original lists: [1, 1, 2, 3, 3, 4, 4, 5] Iterate over all pairs of consecutive items of the said list: [(1, 1), (1, 2), (2, 3), (3, 3), (3, 4), (4, 4), (4, 5)]

Flowchart: Iterate over all pairs of consecutive items in a given list.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource’s quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Reverse a Sequence:

>>> a = (1, 2, 3, 4, 5) >>> a[::-1] (5, 4, 3, 2, 1) >>> b = 'start' >>> b[::-1] 'trats'
  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises
Читайте также:  Пишем нейронную сеть python

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Pair and Triplet Iteration in Python List

In this article, we are going to learn about pair and triplet iteration of a list in Python. Sometimes, it happens that we have to consider two or even three elements of a list to solve a problem. Let’s see how to make iterations of those elements one by one with example. We are going to use list comprehension and zip() function to fulfill our purpose of pair and triplet iteration of a list.

Pair iteration of a Python List

We deliberate over two methods for pair iteration. But first, considering a general list for both the examples, let’s initiate it.

l = [1,2,3,4,5,6,7,8] print("List: ", l) length = len(l) + 1

The given code snippet gives the following output:

Method 1: Using zip() function

zip() function takes iterables as input and clubs the pair at each index of inputted iterables together. See the Python code below:

#Method 1 double_iter = list(zip(l, l[1:])) for i in double_iter: print (i)

Here, we provide the list l as the first parameter and l[1:], i.e., list l without the first element as the second parameter. It gives us the following output:

(1, 2) (2, 3) (3, 4) (4, 5) (5, 6) (6, 7) (7, 8)

Method 2: Using list comprehension

We club i’th and (i+1)’th elements together until the end of the list. Here’s how to do it. The if statement limits the comprehension to end where the list ends.

#Method 2 double_iter = [[(i), (i+1)%length] for i in l if (i+1)%length >= l[1]] for i in double_iter: print (i)
[1, 2] [2, 3] [3, 4] [4, 5] [5, 6] [6, 7] [7, 8]

Now that we’re done with pair iterations let’s see how to consider triplet iterations.

Читайте также:  Javascript сортировка массива функция

Triplet iteration of a Python List

As the zip() function uses only pairs of the iterables, it won’t be useful in this case. But list comprehension is still the most effective way for triplet iteration. We club i’th, (i+1)’th, and (i+2)’th elements together until the end of the list. Just like earlier, the if statement limits the comprehension to end where the list ends.

triple_iter = [[(i), (i+1)%length, (i+2)%length] for i in l if (i+2)%length >= l[2]] for i in triple_iter: print (i)
[1, 2, 3] [2, 3, 4] [3, 4, 5] [4, 5, 6] [5, 6, 7] [6, 7, 8]

This is how we use pairs and triplets of a list to iterate. To get familiar with list comprehension and zip() function refer to the following links:

Источник

Оцените статью