title: Mix two lists python
body: <p>I am trying to create a function to mix two lists in python, or I rather say put the element of list2 into list1. It is required that in the output list, no more than two elements next two each other have the same value</p>
  
  <p>For example:</p>
  
  <pre><code>list1 = [1,1,1,1,1]
  list2 = [2,2,2,2,2]
  output = [1,1,2,1,2,2,1,2,1,2]
  </code></pre>
  
  <p>Wrong output example:</p>
  
  <pre><code># There are more than two '1' standing next two each other
  output = [1,1,1,2,2,1,2,1,2,2]
  </code></pre>
  
  <p>Here is my solution:</p>
  
  <pre><code>def guyGenerator(toughGuy,softGuy):
      i = 0
      while len(softGuy) &gt; 0:
          temp = softGuy[:1]
          while i &lt; len(toughGuy) - 1:
              if toughGuy[i] == toughGuy[i + 1] == 2:
                  toughGuy.insert(random.randint(i, i + 1), temp[0])
              i = i + 1
          softGuy = softGuy[1:]
      return toughGuy
  </code></pre>
  
  <p>The issue is getting the output with more than two of the same elements standing next two each other or the length of the output list is longer than combine length of two lists</p>
  
  <p>For example one of my output</p>
  
  <pre><code>[2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 2]
  </code></pre>
  
  <p>What am I doing wrong here ?</p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>Clodion asked me so I put this up here</p>
  
  <p>The results can be random, as long as they satisfy the requirement in the question. The order of the elements does not matter as long as no more than two elements standing next to each other have the same value</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I am trying to mess around by create a iterable class, using Clodion solution. Here is the new code:</p>
  
  <pre><code>import random
  
  class GuyGenerator:
      def __init__(self,toughGuyList,softGuyList):
          self.toughGuyList = toughGuyList
          self.softGuyList = softGuyList
      def __iter__(self):
          return self
      def __next__(self):
          listSum = self.toughGuyList + self.softGuyList
          while True:
              res = random.sample(listSum,len(listSum))
              if not any([res[i-2]==res[i-1]==res[i] for i in range(len(listSum))]):
                  break
          return res
  
  toughGuy = ['tough','tough','tough','tough','tough','tough','tough','tough']
  softGuy = ['soft','soft','soft','soft','soft','soft','soft','soft']
  for guy in GuyGenerator(toughGuy,softGuy):
      print(guy)
  </code></pre>
  
  <p>The result turned out quite well, except for the code executing unlimited and I have to use KeyboardInterrupt to stop the code. What am I doing wrong in this case ? I would be grateful with a thorough explain since I am a newbie in Python and Iterator</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>Solved the iterator problem, easier than I think. It turned out that what returned in the <strong>iter</strong> is what the class return when calling in the for-loop</p>
  
  <p>Solution:</p>
  
  <pre><code>def __iter__(self):    
      listSum = self.listRandom1 + self.listRandom2
      while True:
          self.res = random.sample(listSum,len(listSum))
          if not any([self.res[i-2]==self.res[i-1]==self.res[i] for i in range(len(listSum))]):
              break
      return iter(self.res)
  </code></pre>