Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Changing Keys for Sids

Hello, Im new in Quantopian and im building my first Algo.
I have a question. I want to change the keys values of a dictionary for sids.
Does anybody knows how to do that?

def initialize(context):  
    #First universe of securities  
    context.stock = {25906: sid(25906), # VHT (Vanguard Health Care ETF)  
                     25905: sid(25905) # VGT (Vanguard Information Technology ETF)  
                     }  
    #Result of best stocks to trade  
    context.variables = {}

    pass


def handle_data(context, data):  
    #Change context.stocks for the best stocks given in the function best_stocks  
    context.variables=best_stocks(context)  
    #New universe of securities  
    context.stocks = context.variables  
    #Order the best stocks  
    #order(context.stocks,100)

#Suppose that in best_stocks the Algo choose the best stocks in the market by some kind of rank  
def best_stocks(context):  
    best = {24: 12, #Apple: qualification  
            46631: 10, #Google: qualification  
            5061: 4 #Microsoft: qualification  
            }

    return best  
1 response

Hi Jose,

You can change the values within a dictionary by doing something like

for key in dictionary:  
    dictionary[key] = new_value  

I would recommend checking out https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Thanks,
Seong

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.