title: Printing routes, denoted by triples, in special order/format in Python
body: <p>I have the following list of lists of triples:</p>
  <pre><code>all_arcs = [[('Start', 2, 1), (2, 7, 1), (7, 'Stop', 1)],
             [('Start', 3, 2), (3, 6, 2), (4, 5, 2), (5, 'Stop', 2), (6, 4, 2)],
             [('Start', 1, 3), (1, 8, 3), (8, 9, 3), (9, 'Stop', 3)]]
  </code></pre>
  <p>Every sub-list in <code>all_arcs</code> is a triplet <code>(i,j,k)</code>, that denotes the tour <code>i-&gt;j</code> of vehicle <code>k</code>. So for example, the first sub-list tells us that vehicle <code>1</code> has the tour <code>Start -&gt; 2 -&gt; 7 -&gt; Stop</code> etc.</p>
  <p>For every sub-list (&lt;=&gt; for every vehicle) in all_arcs I want to print out the tours in the following format:</p>
  <pre><code>Vehicle 1:     Start -&gt; 2 -&gt; 7 -&gt; Stop
  
  Vehicle 2:     Start -&gt; 3 -&gt; 6 -&gt; 4 -&gt; 5 -&gt; Stop
  
  Vehicle 3:     Start -&gt; 1 -&gt; 8 -&gt; 9 -&gt; Stop
  
  ...
  
  (Vehicle n:     Start  -&gt; ...  -&gt; ...  -&gt; Stop) depending on how many sub-list exists.
  </code></pre>
  <p>So I need to write a function that takes <code>all_arcs</code> as parameter and prints out the routes in the format above.</p>
  <h1>My attempt</h1>
  <p>So I thought It would be a good idea to &quot;chain sort&quot; all the sub-lists first. By chain sort I mean to go from <code>(1,2,0),(3,4,0),(2,3,0)</code> to <code>(1,2,0),(2,3,0),(3,4,0)</code>, since the <code>j</code> index in previous triple is always the <code>i</code> index in the next. For the sorting, I used these functions and they worked well:</p>
  <pre><code># function that obtains the next arc-tuple given list of tuples
  def get_next_tuple(tup ,list_of_tuples):
    for i in range(len(list_of_tuples)):
        if tup[1] == list_of_tuples[i][0]:
          next_tuple = list_of_tuples[i]
    return next_tuple
  
  # function that sorts the arc tuples in correct order
  def sort_list_of_tuples(list_of_tuples):
    sorted_list = [list_of_tuples[0]]
    for i in sorted_list:
      if len(sorted_list) &lt; len(list_of_tuples):
        sorted_list.append(get_next_tuple(i, list_of_tuples))
    return sorted_list
  </code></pre>
  <p>Now the printing, this is where I have problems:</p>
  <pre><code>for i, arcs in enumerate(all_arcs):
      sorted_arcs = sort_list_of_tuples(arcs)
      print(f'\nFork-lift {i+1}:')
      for j in range(len(sorted_arcs)):
          print(f'{sorted_arcs[j][0]} -&gt; {sorted_arcs[j][1]}')
      print('\n-----------------------------------')
  </code></pre>
  <p>This obviously works and it prints out the correct order:</p>
  <pre><code>Vehicle 1:
  Start -&gt; 2
  2 -&gt; 7
  7 -&gt; Stop
  
  -----------------------------------
  
  Vehicle 2:
  Start -&gt; 3
  3 -&gt; 6
  6 -&gt; 4
  4 -&gt; 5
  5 -&gt; Stop
  
  -----------------------------------
  
  Vehicle 3:
  Start -&gt; 1
  1 -&gt; 8
  8 -&gt; 9
  9 -&gt; Stop
  
  -----------------------------------
  </code></pre>
  <p>However, I either want it to print like the format above or(and) simply store the routes in simple lists, like this:</p>
  <pre><code>Vehicle 1 = [Start, 2, 7, Stop]
  
  Vehicle 2 = [Start, 3, 6, 4, 5, Stop]
      
  Vehicle 3 = [Start, 1, 8, 9, Stop]
  </code></pre>
  <p>The code is completely reproducible. I appreciate any help.</p>