# Python | Consecutive elements pairing in list

Sometimes, while working with lists, we need to pair up the like elements in list and then store them as lists of list. This particular task has its utility in many domains, be it web development or day-day programming. Let’s discuss certain ways in which this can be achieved.

Method #1 : Using list comprehension
The list comprehension can be easily used to perform this particular task, but consecutively making the pairs of i’th and (i+1)th element.

 `# Python3 code to demonstrate ` `# consecutive element pairing  ` `# using list comprehension ` ` `  `# initializing list ` `test_list ``=` `[``5``, ``4``, ``1``, ``3``, ``2``] ` ` `  `# printing original list ` `print``(``"The original list : "` `+` `str``(test_list)) ` ` `  `# using list comprehension ` `# consecutive element pairing  ` `res ``=` `[[test_list[i], test_list[i ``+` `1``]] ` `        ``for` `i ``in` `range``(``len``(test_list) ``-` `1``)] ` ` `  `# print result ` `print``(``"The consecutive element paired list is : "` `+` `str``(res)) `

Output :

```
The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [[5, 4], [4, 1], [1, 3], [3, 2]]
```

Method #2 : Using `zip() + map()`
This task can also be achieved using the zip function which performs the task for all the elements and map function does the task of pairing of consecutive elements.

 `# Python3 code to demonstrate ` `# consecutive element pairing  ` `# using zip() + map() ` ` `  `# initializing list ` `test_list ``=` `[``5``, ``4``, ``1``, ``3``, ``2``] ` ` `  `# printing original list ` `print``(``"The original list : "` `+` `str``(test_list)) ` ` `  `# using zip() + map() ` `# consecutive element pairing  ` `res ``=` `list``(``map``(``list``, ``zip``(test_list, test_list[``1``:]))) ` ` `  `# print result ` `print``(``"The consecutive element paired list is : "` `+` `str``(res)) `

Output :

```
The original list : [5, 4, 1, 3, 2]
The consecutive element paired list is : [[5, 4], [4, 1], [1, 3], [3, 2]]
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.