title: How to dynamically print list in Python
body: <p>We're working on a Python project, where we are retrieving data from our MySQL database, and we're then sending it back to a new table on our database. We have initialized a list, </p>
  
  <p><code>list_lpn_temp = []</code></p>
  
  <p>The problem is that the range of this list varies, and therefore we don't always know how many datapoints we will have in our list. We have this code, and this is where the error occurs:</p>
  
  <pre><code>df2 = pd.DataFrame(columns=['first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
  df2 = df2.append({'first_temp_lpn' : list_lpn_temp[0][0], 'first_temp_lpn_validated' : list_validated[0], 'second_temp_lpn' : list_lpn_temp[1][0], 'second_temp_lpn_validated' : list_validated[1], 'third_temp_lpn' : list_lpn_temp[2][0], 'third_temp_lpn_validated' : list_validated[2]}, ignore_index=True).round(2)
  
  with engine.connect() as conn, conn.begin():
      df2.to_sql('Raw_Validated', conn, if_exists='append', index=False)
  </code></pre>
  
  <p>Sometimes it gives us an error saying index out of range, as we sometimes only have 2 values in the list, and therefore list_lpn_temp[3][0] will give us the error. Dream scenario would be, if we could somehow send a <em>null</em> or maybe some text saying that we dont have any value to our database. </p>
  
  <p>Therefore we need 2 things:</p>
  
  <ol>
  <li><p>Send data, but where it depends on the size of our list, and is not just set static. For example like this (We need something better than this):</p>
  
  <p>'first_temp_lpn' : list_lpn_temp[0][0]</p></li>
  <li><p>If we are receiving index out of range, then we still need to send something to the database, as it expects 3x columns of temperature. But as there are no values, we could send a <em>null</em>, and therefore this could be nice to implement. Otherwise we will just get another big issue.</p></li>
  </ol>
  
  <p><strong>BIGGER PART OF THE CODE</strong></p>
  
  <pre><code>engine = create_engine("mysql://xxx:xxx@localhost/xxx")
  conn = engine.connect()
  
  list_lpn_temp = []
  
  index = pd.date_range(start=start_range.min(), end=end_range.max(), freq='20T')
  
  for x in index:
      a_temp = pd.read_sql('SELECT temperature FROM Raw_Data', conn).astype(float).values
  
  list_lpn_temp.extend(a_temp)
  
  if len(list_lpn_temp) &gt; max_samples:
      list_lpn_temp.pop(0)
  
  for i in range (len(list_lpn_temp)):
      if -1.5 &lt; 25-list_lpn_temp[i] &lt; 1.5:
          validated_lpn = 1
          list_validated.append(validated_lpn)
          new_list_lpn_temp.extend(list_lpn_temp[i])
      else:
          validated_lpn = 0
          list_validated.append(validated_lpn)
  
  df2 = pd.DataFrame(columns=['first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
  df2 = df2.append({'first_temp_lpn' : list_lpn_temp[0][0], 'first_temp_lpn_validated' : list_validated[0], 'second_temp_lpn' : list_lpn_temp[1][0], 'second_temp_lpn_validated' : list_validated[1], 'third_temp_lpn' : list_lpn_temp[2][0], 'third_temp_lpn_validated' : list_validated[2]}, ignore_index=True).round(2)
  
  with engine.connect() as conn, conn.begin():
      df2.to_sql('Raw_Validated', conn, if_exists='append', index=False)
  </code></pre>
  
  <p><strong>NEW (KP)</strong></p>
  
  <p>We have a time_start and time_end value, which is formatted to datetime. We want to send it with the temp, so we have tried to modify the df2.append. </p>
  
  <pre><code>lastTime = pd.read_sql('SELECT MAX(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()
  firstTime = pd.read_sql('SELECT MIN(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()
  
  firstTime = (pd.to_datetime(firstTime[0])-datetime.timedelta(minutes=10)).round('20T')
  lastTime = (pd.to_datetime(lastTime[0])-datetime.timedelta(minutes=10)).round('20T')
  
  test = lastTime - datetime.timedelta(minutes=40)
  
  time_start = test.astype(str).values[0]
  lastTime = lastTime + datetime.timedelta(minutes=20)
  time_end = lastTime.astype(str).values[0]
  
  for name, value, valid in zip(['first', 'second', 'third'], list_lpn_temp, list_validated):
     temp[name+'_temp_lpn'] = value[0]
     temp[name+'_temp_lpn_validated'] = valid
  
  df2 = df2.append({'time_start' : time_start, 'time_end' : time_end}, temp)        
  
  print (df2)
  </code></pre>
  
  <p>But then only datetime is being sent (time_start and time_end)</p>
  
  <p><a href="https://i.stack.imgur.com/Uyv7O.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uyv7O.png" alt="enter image description here"></a></p>