Reputation: 5
I'm working on my own portfolio and I've ran in some troubles that I can't seem to solve myself or with the help of duckduck go.
Condsider the following dictionary:
def single_dict():
function_dict = {'gld': gld, 'btc': crypto_btc, 'etc': crypto_etc, 'neo': crypto_neo,
'miota': crypto_miota, 'eos': crypto_eos, 'xrp': crypto_xrp, 'nio': stock_nio,
'goldrepublic': gold_republic, 'guldens':guldens, 'maple leafs': maple_leafs}
As you can see, some of the values in the dictionary have the word 'crypto' in their name. Now what I want to do is iterate over the dictionary in such a way that for every value in function_dict python appends the corresponding key to a list.
Would this be possible?
The reason why I'm asking is because I have a function that plots say for instance all crypto related assets, but I wanted to make it so that I don't have to keep changing my crypto plotting function all the time when I buy or sell a new crypto, but rather have it check in function_dict() to see if it needs to be added (based on whether or not it has 'crypto' in it's value name) to a pie chart for example.
Thanks a lot in advance!
Upvotes: 0
Views: 36
Reputation: 2477
function_dict = {'gld': 'gld', 'btc': 'crypto_btc', 'etc': 'crypto_etc', 'neo': 'crypto_neo',
'miota': 'crypto_miota', 'eos': 'crypto_eos', 'xrp': 'crypto_xrp', 'nio': 'stock_nio',
'goldrepublic': 'gold_republic', 'guldens':'guldens', 'maple leafs': 'maple_leafs'}
ans = [key for key,value in function_dict.items() if 'crypto' in value]
Output (All keys that have the substring crypto
in their values) :
>> ans
['btc', 'etc', 'neo', 'miota', 'eos', 'xrp']
Upvotes: 1