- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Consecutive elements pairing in list in Python
During data analysis using python, we may come across a need to pair-up the consecutive elements of a list. In this article we will see the various ways to achieve this.
With index and range
We will design an expression to put the consecutive indexes of the list elements together. And then apply the range function to determine the maximum number of times this pairing of consecutive elements will go on.
Example
listA = [51,23,11,45] # Given list print("Given list A: ", listA) # Use res = [[listA[i], listA[i + 1]] for i in range(len(listA) - 1)] # Result print("The list with paired elements: \n",res)
Output
Running the above code gives us the following result −
Given list A: [51, 23, 11, 45] The list with paired elements: [[51, 23], [23, 11], [11, 45]]
With map and zip
We can also take help of map and zip functions and slicing. We slice the element at position 1 and combine it with elements at position 0. We repeat this for each pair of elements using the zip and map functions.
Example
listA = [51,23,11,45] # Given list print("Given list A: ", listA) # Use zip res = list(map(list, zip(listA, listA[1:]))) # Result print("The list with paired elements: \n",res)
Output
Running the above code gives us the following result −
Given list A: [51, 23, 11, 45] The list with paired elements: [[51, 23], [23, 11], [11, 45]]
- Related Articles
- Python – Cross Pairing in Tuple List
- Python – Consecutive Division in List
- Swap Consecutive Even Elements in Python
- Python – Extract range of Consecutive similar elements ranges from string list
- Python – Filter consecutive elements Tuples
- Python – Reorder for consecutive elements
- Python – Consecutive identical elements count
- Check if array elements are consecutive in Python
- Check if list contains consecutive numbers in Python
- Python – Group Consecutive elements by Sign
- Python – Summation of consecutive elements power
- Check if Queue Elements are pairwise consecutive in Python
- Consecutive Nth column Difference in Tuple List using Python
- Delete List Elements in Python
- Python – Adjacent elements in List

Advertisements