title: How can I optimize this python exercise?
body: <p>I would like to optimize the execution speed of a python exercise:
  A list of position is given, the cursor moves between them. The objective is to find how many time was covered the most covered segment.
  In my example [5, 3, 4, 3, 1, 6] the solution is 4.
  <a href="https://i.stack.imgur.com/q925O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q925O.png" alt="Segment example" /></a></p>
  <p>The result is good, but with more numbers to analyse, it's too long.
  Here is my code:</p>
  <pre><code>liste = [5, 3, 4, 3, 1, 6]
  nbPositions = len(liste)
  listef = [0]*(nbPositions-1)
  for i in range(nbPositions-1):
      valeur1 = liste[i]
      valeur2 = liste[i+1]
      if valeur1 &lt; valeur2:
          for e in range(valeur1, valeur2, 1):
              listef[e-1] += 1
      else:
          for e in range(valeur1, valeur2, -1):
              listef[e-2] += 1
  print(max(listef))
  </code></pre>
  <p><strong>Edit:</strong> Thanks to <a href="https://stackoverflow.com/a/67950166/">@Prune</a> help, here is a version with another approach (it could probably be shorter, but it works):</p>
  <pre><code>liste = [5, 3, 4, 3, 1, 6]
  nbPositions = len(liste)
  liste_i = []
  for i in range(nbPositions-1):
      liste_i.append(liste[i:i+2])
  for i in range(len(liste_i)):
      if liste_i[i][1] &lt; liste_i[i][0]:
          liste_i[i][1], liste_i[i][0] = liste_i[i][0], liste_i[i][1]
  # or 
  # liste_i = [itm if itm[0] &lt; itm[1] else [itm[1], itm[0]] for itm in liste_i]
  # but speed seems the same
  liste_i.sort()
  remplacement = {}
  for i in range(len(liste_i)):
      if not str(liste_i[i]) in remplacement:
          prem = liste_i[i][0]
          deux = liste_i[i][1]
          diff = deux-prem
          if diff &gt; 1:
              rempl = []
              for j in range(diff):
                  rempl.append([prem+j, prem+j+1])
              remplacement[str(liste_i[i])] = rempl
  list3 = []
  for i in range(len(liste_i)):
      if str(liste_i[i]) in remplacement:
          list3.extend(remplacement[str(liste_i[i])])
      else:
          list3.append(liste_i[i])
  plus = max(list3, key=list3.count)
  print(list3.count(plus))
  </code></pre>