I want to make the code shorter.  Is there any way to loop through the stocks and qty dictionaries, access the closing prices of each individual stock and then calculate the current value of the investment preferably in one or two lines?from nsetools import Nsense = Nse()#stock name and buy price pair, but not used in my method below. I would  like a method that uses it though.stocks = {    ADANIPORTS : 284.95,    HINDALCO   : 152.30,    ONGC       : 279.70,    SBIN       : 259.70}#stock name and quantity of stocks     qty = {    ADANIPORTS : 20,    HINDALCO   : 20,    ONGC       : 20,    SBIN       : 20}### Adaniports#Fetches a dictionary with data such as best buy/sell prices, 'closePrice', etc.stock1      = nse.get_quote('ADANIPORTS')#Accessing the 'closePrice' from the above dictionary. stock1Close = stock1['closePrice']### Hindalcostock2      = nse.get_quote('HINDALCO')stock2Close = stock2['closePrice']### ONGCstock3      = nse.get_quote('ONGC')stock3Close = stock3['closePrice']### SBINstock4      = nse.get_quote('SBIN')stock4Close = stock4['closePrice']current_value = (stock1Close * qty['ADANIPORTS']) + (stock2Close * qty['HINDALCO']) + (stock3Close * qty['ONGC']) + (stock4Close * qty['SBIN'])
Using third party library named nsetools for fetching stock data
python;beginner;python 2.7
You never use stocks, so I removed it. It would also help if your qty actually had the exact same keys as you need for nse.get_quote (namely lowercase names), so I did that as well.In addition, you can convert your whole code into one generator, which you can then sum over:from nsetools import Nsense = Nse()def get_closing(stock):    return nse.get_quote(stock)['closePrice']stock_qty = {ADANIPORTS: 20,              HINDALCO: 20,             ONGC: 20,             SBIN: 20 }stock_gen = (get_closing(stock) * qty for stock, qty in stock_qty.items())current_value = sum(stock_gen)If you already have stocks, you could just do:stocks = {    ADANIPORTS : 284.95,    HINDALCO   : 152.30,    ONGC       : 279.70,    SBIN       : 259.70}#stock name and quantity of stocks     qty = {    ADANIPORTS : 20,    HINDALCO   : 20,    ONGC       : 20,    SBIN       : 20}current_value = sum(val * qty[stock] for stock, val in stocks.items())