For Loop Not Respecting Continue Command
<p>The problem I'm having is that the <code>continue</code> command is skipping inconsistently. It skips the numerical output <code>ebitda</code> but puts the incorrect <code>ticker</code> next to it. Why is this? If I make the ticker just <code>phm</code> an input it should skip, it correctly prints an empty list <code>[]</code> but when an invalid ticker is placed next to a valid one, the confusion starts happening. </p>

<pre><code>import requests

ticker = ['aapl', 'phm', 'mmm']
ebitda = []


for i in ticker:

    r_EV=requests.get('https://query2.finance.yahoo.com/v10/finance/quoteSummary/'+i+'?formatted=true&amp;crumb=8ldhetOu7RJ&amp;lang=en-US&amp;region=US&amp;modules=defaultKeyStatistics%2CfinancialData%2CcalendarEvents&amp;corsDomain=finance.yahoo.com')
    r_ebit = requests.get('https://query1.finance.yahoo.com/v10/finance/quoteSummary/' + i + '?formatted=true&amp;crumb=B2JsfXH.lpf&amp;lang=en-US&amp;region=US&amp;modules=incomeStatementHistory%2CcashflowStatementHistory%2CbalanceSheetHistory%2CincomeStatementHistoryQuarterly%2CcashflowStatementHistoryQuarterly%2CbalanceSheetHistoryQuarterly%2Cearnings&amp;corsDomain=finance.yahoo.com%27')

    data = r_EV.json()
    data1 = r_ebit.json()

    if data1['quoteSummary']['result'][0]['balanceSheetHistoryQuarterly']['balanceSheetStatements'][0].get('totalCurrentAssets') == None:
        continue #skips certain ticker if no Total Current Assets available (like for PHM)

    ebitda_data = data['quoteSummary']['result'][0]['financialData']
    ebitda_dict = ebitda_data['ebitda']
    ebitda.append(ebitda_dict['raw']) #navigates to dictionairy where ebitda is stored

ebitda_formatted = dict(zip(ticker, ebitda))

print(ebitda_formatted)
# should print {'aapl': 73961996288, 'mmm': 8618000384}
# NOT: {'aapl': 73961996288, 'phm': 8618000384}
</code></pre>

<p>The <code>continue</code> works just fine. You produce this list:</p>

<pre><code>[73961996288, 8618000384]
</code></pre>

<p>However, you then zip that list with <code>ticker</code>, which still has 3 elements in it, including <code>'phm'</code>. <code>zip()</code> stops when one of the iterables is empty, so you produce the following tuples:</p>

<pre><code>&gt;&gt;&gt; ebitda
[73961996288, 8618000384]
&gt;&gt;&gt; ticker
['aapl', 'phm', 'mmm']
&gt;&gt;&gt; zip(ticker, ebitda)
[('aapl', 73961996288), ('phm', 8618000384)]
</code></pre>

<p>If you are <em>selectively</em> adding  <code>ebitda</code> values to a list, you'd also have to record what <code>ticker</code> values you processed:</p>

<pre><code>used_ticker.append(i)
</code></pre>

<p>and use that new list.</p>

<p>Or you could just <em>start</em> with an empty <code>ebitda_formatted</code> dictionary and add to that in the loop:</p>

<pre><code>ebitda_formatted[i] = ebitda_dict['raw']
</code></pre>

python for-loop continue