{"id":63,"date":"2022-04-12T23:46:21","date_gmt":"2022-04-12T23:46:21","guid":{"rendered":"https:\/\/codecrypt76.com\/?p=63"},"modified":"2022-11-25T16:49:57","modified_gmt":"2022-11-25T16:49:57","slug":"compare-adjacent-elements-in-a-list","status":"publish","type":"post","link":"https:\/\/codecrypt76.com\/index.php\/2022\/04\/12\/compare-adjacent-elements-in-a-list\/","title":{"rendered":"Compare Adjacent Elements in an Array"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"960\" height=\"440\" src=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/featuredimage-7.jpg\" alt=\"\" class=\"wp-image-92\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/featuredimage-7.jpg 960w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/featuredimage-7-300x138.jpg 300w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/featuredimage-7-768x352.jpg 768w\" sizes=\"(max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2>Introduction<\/h2>\n\n\n\n<p>This article will discuss how to compare adjacent elements in a List using Python. Suppose you want to create a new list from a given list by calculating the difference between adjacent elements of in the list, for example:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Input<\/strong>: test_list = [26, 19, 12, 6]<\/p><p><strong>Output<\/strong>: new_list = [7, 7, 6]<\/p><p><strong>Explanation<\/strong>:<br><br>26 \u2013 19 = 7<br><br>19 \u2013 12 = 7<br><br>12 \u2013 6 = 6<\/p><\/blockquote>\n\n\n\n<p>There are several methods to achieve this. Each method demonstrated will use the same input list of&nbsp;<code>[26, 19, 12, 6]<\/code>&nbsp;and produce the same output of&nbsp;<code>[7, 7, 6]<\/code><\/p>\n\n\n\n<h2>Method 1: Using the&nbsp;<code>range()<\/code>&nbsp;function:<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def adjacent_items(seq):\n    new_list = []\n    for i in range(1, len(seq)):\n        previous_item = seq[i - 1]\n        current_item = seq[i]\n        new_list.append(previous_item - current_item)\n\n    return new_list   # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<h2>Method 2: Using the&nbsp;<code>zip()<\/code>&nbsp;function and&nbsp;<code>slices<\/code>:<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def adjacent_items(seq):\n    new_list = []\n    list1 = seq[0::]  # Copy of list starting at index 0\n    list2 = seq[1::]  # copy of list starting at index 1       \n\n    for previous_item, current_item in zip(list1, list2):\n        new_list.append(previous_item - current_item)\n    return new_list  # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<h2>Method 3: Using the&nbsp;<code>zip()<\/code>&nbsp;function,&nbsp;<code>slices<\/code>, and a&nbsp;<code>list comprehension<\/code>:<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>new_list = [previous_item - current_item \n            for (previous_item, current_item) \n            in zip(test_list[0::], test_list[1::])]\nprint(new_list)  # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<h2>Method 4: Using a&nbsp;<code>generator<\/code>&nbsp;and&nbsp;<code>list comprehension<\/code>:<\/h2>\n\n\n\n<p>First lets create the generator.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def adjacent_items(seq):\n    iterable_list = iter(seq)\n    prev = next(iterable_list)\n    for item in iterable_list:\n        yield (prev - item)\n        prev = item<\/code><\/pre><\/div>\n\n\n\n<ul><li><strong>Note<\/strong>: Since this is a&nbsp;<code>generator<\/code>, you must iterate over it to use it. We\u2019ll use a&nbsp;<code>list comprehension<\/code>&nbsp;to produce the results:<\/li><\/ul>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>new_list = [item for item in adjacent_items(test_list)]\nprint(new_list)  # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<h2>Method 5: Using the&nbsp;<code>more_itertool<\/code>&nbsp;and&nbsp;<code>pairwise()<\/code>&nbsp;function:<\/h2>\n\n\n\n<ul><li><strong>Note<\/strong>: You must use&nbsp;<code>pip<\/code>&nbsp;to install&nbsp;<code>more_itertools<\/code><\/li><\/ul>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>pip install more_itertools\n<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from more_itertools import pairwise\n\ndef adjacent_items(seq):\n    new_list = []\n    for previous_item, current_item in pairwise(lst):\n        new_list.append(previous_item - current_item)\n    return new_list  # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<p>Of course the above example can be achieved using a list comprehension as well.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from more_itertools import pairwise\n\nnew_list = [previous_item - current_item\n            for (previous_item , current_item)\n            in pairwise(test_list)]\nprint(new_list)  # Output [7, 7, 6]<\/code><\/pre><\/div>\n\n\n\n<p>For more information on&nbsp;<code>more_itertools<\/code>&nbsp;visit&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/itertools.html#itertools-recipes\/\">Python docs<\/a>.<\/p>\n\n\n\n<h2><a href=\"https:\/\/dev.to\/seraph776\/how-to-compare-adjacent-elements-in-a-list-with-python-4o7e-temp-slug-7310257?preview=83b8973809a5047e2763002e71bce6cb6503f7a281abb0dd1bbc868ba261bc7b11172e383ed3fbb51edf5941f26773c116c3cf2fbef6b178e7fc6c31#conclusion\"><\/a>Conclusion<\/h2>\n\n\n\n<p>The purpose of this article was to demonstrate how to compare adjacent elements in a List with Python. The code examples used in this article all create a new list from a given list by calculating the difference between adjacent elements of in the list. However, these functions can modify to suit your purpose. Hopefully you have a better understanding on how to compare adjacent elements in a List using Python. There are other solutions; these are just a few. If you found this article helpful or have any feedback, please leave a comment. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn several methods on how to compare adjacent elements in an array using Python.<\/p>\n","protected":false},"author":1,"featured_media":92,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[9,29,27,30,11],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/63"}],"collection":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":8,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media\/92"}],"wp:attachment":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}