{"id":14283,"date":"2021-07-06T08:11:37","date_gmt":"2021-07-06T02:41:37","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=14283"},"modified":"2021-07-06T08:11:40","modified_gmt":"2021-07-06T02:41:40","slug":"run-length-encoding-python","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/","title":{"rendered":"Find Out What is Run Length Encoding in Python"},"content":{"rendered":"\n<p>Run length encoding in python is an algorithm using which we replace values inside a string that occurs repetitively. We count the number of similar characters, and instead of sending them multiple times, we send them along with their character count.  So, for every character of similar value, only two characters are representing it &#8211; its value and its count. In this article, we shall build a function for implementing <strong>run length encoding in python.<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#What_is_the_need_of_run_length_encoding\" >What is the need of run length encoding?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#Implementing_run_length_encoding\" >Implementing run length encoding<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#1_Defining_the_function\" >1. Defining the function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#2_Calling_the_function\" >2. Calling the function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#The_Entire_Code_is\" >The Entire Code is:<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#How_to_do_decoding\" >How to do decoding?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#Run_length_encoding_using_numpy\" >Run length encoding using numpy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#Trending_Right_Now\" >Trending Right Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-need-of-run-length-encoding\"><span class=\"ez-toc-section\" id=\"What_is_the_need_of_run_length_encoding\"><\/span>What is the need of run length encoding?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>Run length encoding is an algorithm for performing lossless data compression. Lossless data compression refers to compressing the data in such a way that the original form of the data can then be derived from it. When a character occurs a large number of times consecutively in a sequence, then we can represent the same consecutive subsequence using only a single occurrence of that character and its<a href=\"http:\/\/www.pythonpool.com\/numpy-count\/\" target=\"_blank\" rel=\"noreferrer noopener\"> <span style=\"text-decoration: underline;\">count<\/span><\/a>. Using run length encoding, we can save memory space while transmitting data and preserving its original form. It is useful when we want to store or transmit large sequences of data. <\/strong><\/p>\n\n\n\n<p><strong>For example if the sequence is : AACCCBBBBBAAAAFFFFFFFF<\/strong><\/p>\n\n\n\n<p><strong>Then, using run length encoding, we can represent it as: A2C3B5A4F8<\/strong><\/p>\n\n\n\n<p><strong>The 22 length sequence was compressed to a 10 length sequence. <\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-implementing-run-length-encoding\"><span class=\"ez-toc-section\" id=\"Implementing_run_length_encoding\"><\/span>Implementing run length encoding<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To implement run length encoding, we will have to store the string first. Then, we have to scan the entire string, store each consecutive character by a single character, and count its occurrences. We will implement run length encoding in python using the list.<\/p>\n\n\n\n<p>Implementing run length encoding using a list can be done using indexing. We can either maintain two different lists &#8211; one for storing character and one for storing its count or use nested lists inside a single list. Here, we shall perform the program using nested lists. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-defining-the-function\"><span class=\"ez-toc-section\" id=\"1_Defining_the_function\"><\/span>1. Defining the function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Now first, we shall define the run length encoding function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef run_length_encoding(seq):\n  compressed = &#x5B;]\n  count = 1\n  char = seq&#x5B;0]\n  for i in range(1,len(seq)):\n    if seq&#x5B;i] == char:\n      count = count + 1\n    else :\n      compressed.append(&#x5B;char,count])\n      char = seq&#x5B;i]\n      count = 1\n  compressed.append(&#x5B;char,count])\n  return compressed\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<p>Here, the name of the function defined is <em>&#8216;run_length_encoding()&#8217;<\/em>. It accepts one argument, which is the sequence to be compressed. We have taken an empty list named<em> &#8216;compressed&#8217;<\/em> to which we will append sub-lists. Then, we have taken a counter variable named<em> &#8216;count&#8217;<\/em> which will count the character occurrences. The <em>&#8216;count&#8217;<\/em> is set to 1. <\/p>\n\n\n\n<p>Also, we have a string named <em>&#8216;char&#8217; <\/em>which will be used to store the previous character, and we shall compare the current character with &#8216;char.&#8217; Initially, the value of variable<em> &#8216;char&#8217;<\/em> is the first character of the sequence. <\/p>\n\n\n\n<p>Then, we run a for loop from the second character of the <a href=\"https:\/\/www.britannica.com\/technology\/sequence-programming#:~:text=Sequence%20is%20the%20default%20control,%2B%20bx%20%2B%20c%20%3D%200.\" target=\"_blank\" rel=\"noreferrer noopener\">sequence<\/a> till the end of the sequence. Using an if condition, we shall compare if the current character is equal to the character stored in the variable<em> &#8216;char.&#8217;<\/em> If equal, then it means that two consecutive characters have the same value, so we will then increment &#8216;count.&#8217;<em> <\/em><\/p>\n\n\n\n<p>The count will be incremented till two consecutive characters are equal. Once that condition fails, the program will move to the other part. So, it will append a nested list to the main list <em>&#8216;compressed.&#8217;<\/em> The nested list shall contain the character and its count. <\/p>\n\n\n\n<p>Then, we shall set the value of <em>&#8216;char&#8217; <\/em>to the new character and set <em>&#8216;count&#8217; <\/em>to 1. Once the complete list has been scanned, we shall append the last character sequence to the list<em> &#8216;compressed,&#8217;<\/em> otherwise remaining excluded from the list. <\/p>\n\n\n\n<p>In the end, we return the list<em> &#8216;compressed.&#8217;<\/em><\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-alpha monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-calling-the-function\"><span class=\"ez-toc-section\" id=\"2_Calling_the_function\"><\/span>2. Calling the function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>First, we shall define a string sequence inside a variable<em> &#8216;seq.&#8217; <\/em>Then, we shall pass that sequence into the <em>run_length_encoding()<\/em> function and store the output string into a list named<em> &#8216;list1&#8217;.<\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nseq = 'AACCCBBBBBAAAAFFFFFFFF'\nlist1 = run_length_encoding(seq)\n<\/pre><\/div>\n\n\n<p>The list1 would be something like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;'A', 2], &#x5B;'C', 3], &#x5B;'B', 5], &#x5B;'A', 4], &#x5B;'F', 8]]\n<\/pre><\/div>\n\n\n<p>Now, we shall take a null string named <em>&#8216;compressed_seq.&#8217;<\/em> Using for loop, we will print each nested list and store its value in the form of a string inside &#8216;compressed_seq.&#8217;<em>  <\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncompressed_seq = ''\n\nfor i in range(0,len(list1)):\n  for j in list1&#x5B;i]:\n    compressed_seq += str(j)\n\nprint(compressed_seq)\n<\/pre><\/div>\n\n\n<p><strong>The output compressed string after encoding is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A2C3B5A4F8<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-the-entire-code-is\"><span class=\"ez-toc-section\" id=\"The_Entire_Code_is\"><\/span>The Entire Code is:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef run_length_encoding(seq):\n  compressed = &#x5B;]\n  count = 1\n  char = seq&#x5B;0]\n  for i in range(1,len(seq)):\n    if seq&#x5B;i] == char:\n      count = count + 1\n    else :\n      compressed.append(&#x5B;char,count])\n      char = seq&#x5B;i]\n      count = 1\n  compressed.append(&#x5B;char,count])\n  return compressed\n\nseq = 'AACCCBBBBBAAAAFFFFFFFF'\nlist1 = run_length_encoding(seq)\n\ncompressed_seq = ''\n\nfor i in range(0,len(list1)):\n  for j in list1&#x5B;i]:\n    compressed_seq += str(j)\n\nprint(compressed_seq)\n<\/pre><\/div>\n\n\n<p class=\"has-background\" style=\"background-color:#d4e1ec;font-size:19px\"><strong>Also, Read |<\/strong> <a href=\"http:\/\/www.pythonpool.com\/python-list-length\/\" rel=\"noreferrer noopener\" target=\"_blank\"><span style=\"text-decoration: underline;\">Python List Length | How to Find the Length of List in Python<\/span><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-do-decoding\"><span class=\"ez-toc-section\" id=\"How_to_do_decoding\"><\/span>How to do decoding?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Decoding the compressed string is not a very complex task. We shall define a function named<em> &#8216;run_length_decoding()&#8217; <\/em>to perform decoding. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef run_length_decoding(compressed_seq):\n  seq = ''\n  for i in range(0,len(compressed_seq)):\n    if compressed_seq&#x5B;i].isalpha() == True:\n      for j in range(int(compressed_seq&#x5B;i+1])):\n        seq += compressed_seq&#x5B;i]\n\n  return(seq)\n<\/pre><\/div>\n\n\n<p>The function will take one argument, &#8216;compressed_seq,&#8217; which will be the sequence in its compressed form. We take a local null string named &#8216;seq.&#8217; Then, we will run a for loop along the length of the compressed sequence. We will check whether the current character is an alphabet or not. We will check that using<em> the isalpha() <\/em>method. <\/p>\n\n\n\n<p>Since the compressed representation contains the character first and then its count, we will first check for the alphabet character. If it is an alphabet, then the next character will be its count. So, we will run another for loop until the count&#8217;s value and append that character to the string<em> &#8216;seq&#8217;.<\/em> In the end, we shall return the string<em> &#8216;seq&#8217;.<\/em><\/p>\n\n\n\n<p>Then we will call the function<em> run_length_decoding() <\/em>and pass the compressed string as an argument to that function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(run_length_decoding(compressed_seq))\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">AACCCBBBBBAAAAFFFFFFFF\n<\/pre>\n\n\n\n<p><strong> As seen, this will return the original sequence. <\/strong><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>The entire program containing for implementing both encoding and decoding is :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef run_length_encoding(seq):\n  compressed = &#x5B;]\n  count = 1\n  char = seq&#x5B;0]\n  for i in range(1,len(seq)):\n    if seq&#x5B;i] == char:\n      count = count + 1\n    else :\n      compressed.append(&#x5B;char,count])\n      char = seq&#x5B;i]\n      count = 1\n  compressed.append(&#x5B;char,count])\n  return compressed\n\ndef run_length_decoding(compressed_seq):\n  seq = ''\n  for i in range(0,len(compressed_seq)):\n    if compressed_seq&#x5B;i].isalpha() == True:\n      for j in range(int(compressed_seq&#x5B;i+1])):\n        seq += compressed_seq&#x5B;i]\n\n  return(seq)\n\nseq = 'AACCCBBBBBAAAAFFFFFFFF'\nlist1 = run_length_encoding(seq)\n\ncompressed_seq = ''\n\nfor i in range(0,len(list1)):\n  for j in list1&#x5B;i]:\n    compressed_seq += str(j)\n\nprint(compressed_seq)\nprint(run_length_decoding(compressed_seq))\n<\/pre><\/div>\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-golf monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-run-length-encoding-using-numpy\"><span class=\"ez-toc-section\" id=\"Run_length_encoding_using_numpy\"><\/span>Run length encoding using numpy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can also perform run length encoding on a numpy array. Let us create a separate function for that. The name of the function is <em>run_length_encoding_numpy()<\/em>. It accepts one argument, which is the array sequence. <\/p>\n\n\n\n<p><strong>First, we shall import the numpy library. <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Then, the function defined is:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef run_length_encoding_numpy(seq_array):\n  compressed_seq = ''\n  change =  (seq_array&#x5B;1:] != seq_array&#x5B;:-1])\n  x = np.append(np.where(change), len(seq_array)-1)\n  counter = np.diff(np.append(-1,x))\n\n  for i in range(0,len(counter)):\n    compressed_seq = compressed_seq + str(seq_array&#x5B;x]&#x5B;i]) + str(counter&#x5B;i])\n\n  return compressed_seq\n<\/pre><\/div>\n\n\n<p>The variable<em> &#8216;change&#8217;<\/em> is a list containing boolean values. The value is <em>False<\/em> if a given sequence has the same consecutive values. It will be True where the values are changing from their previous value. <em>np.where(change)<\/em> shall contain the indexes where the list change was <em>True<\/em>. Then, we have a variable named<em> &#8216;counter&#8217; <\/em>which will store the count of consecutively equal sequences. We shall add the array element and its respective count to the string<em> &#8216;compressed_seq&#8217; using a for loop<\/em>. The function shall return that string. <\/p>\n\n\n\n<p><strong>To call the function, we shall execute the below lines of code.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr_array = np.array(&#x5B;'A','A','B','C','C','C','C','C'])\nprint(run_length_encoding_numpy(str_array))\n<\/pre><\/div>\n\n\n<p><strong>The output is:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">A2B1C5<\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p>That sums up everything about Run Length Encoding in Python. If you have any questions in your mind or any thoughts to share, do let us know in the comments below.<\/p>\n\n\n\n<p><em>Until next time, Keep Learning!<\/em><\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-charlie monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-1\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Right_Now\"><\/span>Trending Right Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"font-size:18px;\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"font-size:18px;\">[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"font-size:18px;\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"background-color:#EEEEEE;border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"font-size:18px;\">[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Run length encoding in python is an algorithm using which we replace values inside a string that occurs repetitively. We count the number of similar &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Find Out What is Run Length Encoding in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#more-14283\" aria-label=\"More on Find Out What is Run Length Encoding in Python\">Read more<\/a><\/p>\n","protected":false},"author":20,"featured_media":14305,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4315,4313,4314],"class_list":["post-14283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-run-length-encoding","tag-run-length-encoding-in-python","tag-run-length-encoding-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find Out What is Run Length Encoding in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Run length encoding in python is an algorithm for replacing a consecutive subsequence by a single occurrence of that character and its count.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find Out What is Run Length Encoding in Python\" \/>\n<meta property=\"og:description\" content=\"Run length encoding in python is an algorithm using which we replace values inside a string that occurs repetitively. We count the number of similar\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-06T02:41:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-06T02:41:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Dhruvi Vikma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dhruvi Vikma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\"},\"author\":{\"name\":\"Dhruvi Vikma\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\"},\"headline\":\"Find Out What is Run Length Encoding in Python\",\"datePublished\":\"2021-07-06T02:41:37+00:00\",\"dateModified\":\"2021-07-06T02:41:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\"},\"wordCount\":1073,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg\",\"keywords\":[\"python run length encoding\",\"run length encoding in python\",\"run-length encoding python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\",\"url\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\",\"name\":\"Find Out What is Run Length Encoding in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg\",\"datePublished\":\"2021-07-06T02:41:37+00:00\",\"dateModified\":\"2021-07-06T02:41:40+00:00\",\"description\":\"Run length encoding in python is an algorithm for replacing a consecutive subsequence by a single occurrence of that character and its count.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg\",\"width\":1200,\"height\":628,\"caption\":\"run length encoding python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Find Out What is Run Length Encoding in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23\",\"name\":\"Dhruvi Vikma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g\",\"caption\":\"Dhruvi Vikma\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Find Out What is Run Length Encoding in Python - Python Pool","description":"Run length encoding in python is an algorithm for replacing a consecutive subsequence by a single occurrence of that character and its count.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/","og_locale":"en_US","og_type":"article","og_title":"Find Out What is Run Length Encoding in Python","og_description":"Run length encoding in python is an algorithm using which we replace values inside a string that occurs repetitively. We count the number of similar","og_url":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/","og_site_name":"Python Pool","article_published_time":"2021-07-06T02:41:37+00:00","article_modified_time":"2021-07-06T02:41:40+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg","type":"image\/jpeg"}],"author":"Dhruvi Vikma","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Dhruvi Vikma","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/"},"author":{"name":"Dhruvi Vikma","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23"},"headline":"Find Out What is Run Length Encoding in Python","datePublished":"2021-07-06T02:41:37+00:00","dateModified":"2021-07-06T02:41:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/"},"wordCount":1073,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg","keywords":["python run length encoding","run length encoding in python","run-length encoding python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/run-length-encoding-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/","url":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/","name":"Find Out What is Run Length Encoding in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg","datePublished":"2021-07-06T02:41:37+00:00","dateModified":"2021-07-06T02:41:40+00:00","description":"Run length encoding in python is an algorithm for replacing a consecutive subsequence by a single occurrence of that character and its count.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/run-length-encoding-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Find-Out-What-is-Run-Length-Encoding-in-Python.jpg","width":1200,"height":628,"caption":"run length encoding python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/run-length-encoding-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Find Out What is Run Length Encoding in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/bed8fc40c7b71baf7d76b1cfefd79f23","name":"Dhruvi Vikma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/520d8a37e8b6ce4e6df57e997dc0d571225f48098bbb02838c3884bb603e886f?s=96&d=wavatar&r=g","caption":"Dhruvi Vikma"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=14283"}],"version-history":[{"count":19,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14283\/revisions"}],"predecessor-version":[{"id":14309,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14283\/revisions\/14309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/14305"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=14283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=14283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=14283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}