title: Dynamic list length leads to "IndexError: list index out of range" with pandas.DataFrame - how to best avoid?
body: <p>I try to build a <code>pd.DataFrame</code> with dates stored in a list. Before the list is handed over to pandas it is appended or not depending on conditions, changing it's length. Example code:</p>
  <pre class="lang-py prettyprint-override"><code>import pandas as pd
  import datetime
  
  start_date = datetime.date(2025, 1, 1)
  
  date_lst = [datetime.date(2020, 1, 1), datetime.date(2021, 1, 1)]
  condition = 0
  
  
  if date_lst[0] &gt; start_date:
      date_lst.append(datetime.date(2022, 1, 1))
      date_lst.append(datetime.date(2023, 1, 1))
  else:
      pass
  
  df = pd.DataFrame([
          dict(Task=&quot;DateRange 1&quot;, Start=date_lst[0], End=date_lst[1]),
          dict(Task=&quot;DateRange 2&quot;, Start=date_lst[2], End=date_lst[3]),
      ])
  
  print(df)
  </code></pre>
  <p>This gives the working output:</p>
  <pre><code>          Task       Start         End
  0  DateRange 1  2020-01-01  2021-01-01
  1  DateRange 2  2022-01-01  2023-01-01
  </code></pre>
  <p>However, if the condition is not met, e.g. if <code>start_date</code> is changed to <code>datetime.date(2025, 1, 1)</code>, the final list has only two entries. Index [2] and [3] cannot be called and it returns the error:</p>
  <pre><code>IndexError: list index out of range
  </code></pre>
  <p>Is there a way to avoid this outcome while still visualizing all dates available? Any help is much appreciated.</p>