Trying to use nested for loop to correctly parse nested list items
Question:
I have a nested list (derived from a psycopg2 psql query) that contains a list of company names, stock tickers, and industries. I am using a subsequent nested for loop to try and extract the 1st (company name) and 3rd (industry) elements from the nested list rows in subsequent print statements.
In this example, the company name is American Airlines, the stock ticker is AAL, and the industry is Airlines.
postgreSQL query:
# psycopg2 query to pull company name and industry based on stock ticker
cur.execute('SELECT company,stockTicker,industry FROM stockNames WHERE
stockTicker = %s',(stockChoice,))
records=cur.fetchall()
My current nested for loop is as follows:
for i in records:
for j in i:
count=0
if count==0:
print('n' 'Company Name: ',j)
print('Stock Ticker: ',stockChoice)
continue
if count==1:
continue
if count==2:
print('Industry: ',list,j)
print('Stock Price: ',stockPrice.text)
continue
count+=1
And the output is:
Company Name: American Airlines
Stock Ticker: AAL
Company Name: AAL
Stock Ticker: AAL
Company Name: Airlines
Stock Ticker: AAL
My expected output is this:
Company Name: American Airlines
Stock Ticker: AAL
Industry: Airlines
Any help on this is appreciated to get the control flow right for what I’m trying to do.
Answers:
Don’t use nested loops. Just unpack each row into variables
for company, ticker, industry in records:
print(f'Company name: {company}nStock ticker: {ticker}nIndustry: {industry}n')
And if you’re only getting the information for one stock, there’s no need for the loop at all.
cur.execute('SELECT company,stockTicker,industry FROM stockNames WHERE stockTicker = %s',(stockChoice,))
row = cur.fetchone()
if row:
company, ticker, industry = row
print(f'Company name: {company}nStock ticker: {ticker}nIndustry: {industry}n')
I have a nested list (derived from a psycopg2 psql query) that contains a list of company names, stock tickers, and industries. I am using a subsequent nested for loop to try and extract the 1st (company name) and 3rd (industry) elements from the nested list rows in subsequent print statements.
In this example, the company name is American Airlines, the stock ticker is AAL, and the industry is Airlines.
postgreSQL query:
# psycopg2 query to pull company name and industry based on stock ticker
cur.execute('SELECT company,stockTicker,industry FROM stockNames WHERE
stockTicker = %s',(stockChoice,))
records=cur.fetchall()
My current nested for loop is as follows:
for i in records:
for j in i:
count=0
if count==0:
print('n' 'Company Name: ',j)
print('Stock Ticker: ',stockChoice)
continue
if count==1:
continue
if count==2:
print('Industry: ',list,j)
print('Stock Price: ',stockPrice.text)
continue
count+=1
And the output is:
Company Name: American Airlines
Stock Ticker: AAL
Company Name: AAL
Stock Ticker: AAL
Company Name: Airlines
Stock Ticker: AAL
My expected output is this:
Company Name: American Airlines
Stock Ticker: AAL
Industry: Airlines
Any help on this is appreciated to get the control flow right for what I’m trying to do.
Don’t use nested loops. Just unpack each row into variables
for company, ticker, industry in records:
print(f'Company name: {company}nStock ticker: {ticker}nIndustry: {industry}n')
And if you’re only getting the information for one stock, there’s no need for the loop at all.
cur.execute('SELECT company,stockTicker,industry FROM stockNames WHERE stockTicker = %s',(stockChoice,))
row = cur.fetchone()
if row:
company, ticker, industry = row
print(f'Company name: {company}nStock ticker: {ticker}nIndustry: {industry}n')