I have a dictionary like this
stocks= {'AALI': {'company': 'Astra Agro Lestari Tbk.', 'CDLDOJI': None},
'ABBA': {'company': 'Mahaka Media Tbk.', 'CDLDOJI': 'bullish'},
'ABDA': {'company': 'Asuransi Bina Dana Arta Tbk.', 'CDLDOJI': None},
'ABMM': {'company': 'ABM Investama Tbk.', 'CDLDOJI': 'bullish'},
'ACES': {'company': 'Ace Hardware Indonesia Tbk.', 'CDLDOJI': None},
'ACST': {'company': 'Acset Indonusa Tbk.', 'CDLDOJI': None},
'ADES': {'company': 'Akasha Wira International Tbk.', 'CDLDOJI': None},
'ADHI': {'company': 'Adhi Karya (Persero) Tbk.', 'CDLDOJI': None},
'ADMF': {'company': 'Adira Dinamika Multi Finance T', 'CDLDOJI': 'bullish'},
'ADMG': {'company': 'Polychem Indonesia Tbk', 'CDLDOJI': None},
'ADRO': {'company': 'Adaro Energy Tbk.', 'CDLDOJI': 'bullish'},
'AGAR': {'company': 'Asia Sejahtera Mina Tbk.', 'CDLDOJI': None},
'AGII': {'company': 'Aneka Gas Industri Tbk.', 'CDLDOJI': None},
'AGRO': {'company': 'Bank Raya Indonesia Tbk.', 'CDLDOJI': None},
'AGRS': {'company': 'Bank IBK Indonesia Tbk.', 'CDLDOJI': 'bullish'},
'AHAP': {'company': 'Asuransi Harta Aman Pratama Tb', 'CDLDOJI': None},
'AIMS': {'company': 'Akbar Indo Makmur Stimec Tbk', 'CDLDOJI': None},
'AISA': {'company': 'FKS Food Sejahtera Tbk.', 'CDLDOJI': None},
'AKKU': {'company': 'Anugerah Kagum Karya Utama Tbk', 'CDLDOJI': 'bullish'},
'AKPI': {'company': 'Argha Karya Prima Industry Tbk', 'CDLDOJI': None},
'AKRA': {'company': 'AKR Corporindo Tbk.', 'CDLDOJI': None}}
I want to make a new dictionary that only consist of the stocks that have value in the 'CDLDOJI', I want to remove all the stocks that is None in the new dictionary. How to do that?
CodePudding user response:
Here's a dictionary comprehension that does that.
stocks = {
'AALI': {'company': 'Astra Agro Lestari Tbk.', 'CDLDOJI': None},
'ABBA': {'company': 'Mahaka Media Tbk.', 'CDLDOJI': 'bullish'},
'ABDA': {'company': 'Asuransi Bina Dana Arta Tbk.', 'CDLDOJI': None},
'ABMM': {'company': 'ABM Investama Tbk.', 'CDLDOJI': 'bullish'},
'ACES': {'company': 'Ace Hardware Indonesia Tbk.', 'CDLDOJI': None},
'ACST': {'company': 'Acset Indonusa Tbk.', 'CDLDOJI': None},
'ADES': {'company': 'Akasha Wira International Tbk.', 'CDLDOJI': None},
'ADHI': {'company': 'Adhi Karya (Persero) Tbk.', 'CDLDOJI': None},
'ADMF': {'company': 'Adira Dinamika Multi Finance T', 'CDLDOJI': 'bullish'},
'ADMG': {'company': 'Polychem Indonesia Tbk', 'CDLDOJI': None},
'ADRO': {'company': 'Adaro Energy Tbk.', 'CDLDOJI': 'bullish'},
'AGAR': {'company': 'Asia Sejahtera Mina Tbk.', 'CDLDOJI': None},
'AGII': {'company': 'Aneka Gas Industri Tbk.', 'CDLDOJI': None},
'AGRO': {'company': 'Bank Raya Indonesia Tbk.', 'CDLDOJI': None},
'AGRS': {'company': 'Bank IBK Indonesia Tbk.', 'CDLDOJI': 'bullish'},
'AHAP': {'company': 'Asuransi Harta Aman Pratama Tb', 'CDLDOJI': None},
'AIMS': {'company': 'Akbar Indo Makmur Stimec Tbk', 'CDLDOJI': None},
'AISA': {'company': 'FKS Food Sejahtera Tbk.', 'CDLDOJI': None},
'AKKU': {'company': 'Anugerah Kagum Karya Utama Tbk', 'CDLDOJI': 'bullish'},
'AKPI': {'company': 'Argha Karya Prima Industry Tbk', 'CDLDOJI': None},
'AKRA': {'company': 'AKR Corporindo Tbk.', 'CDLDOJI': None}
}
stocks = {k: v for k, v in stocks.items() if v["CDLDOJI"] is not None}
Printing the new stocks
yields
{
'ABBA': {'CDLDOJI': 'bullish', 'company': 'Mahaka Media Tbk.'},
'ABMM': {'CDLDOJI': 'bullish', 'company': 'ABM Investama Tbk.'},
'ADMF': {'CDLDOJI': 'bullish', 'company': 'Adira Dinamika Multi Finance T'},
'ADRO': {'CDLDOJI': 'bullish', 'company': 'Adaro Energy Tbk.'},
'AGRS': {'CDLDOJI': 'bullish', 'company': 'Bank IBK Indonesia Tbk.'},
'AKKU': {'CDLDOJI': 'bullish', 'company': 'Anugerah Kagum Karya Utama Tbk'}
}
CodePudding user response:
You can also use filter
and dict
functions:
out = dict(filter(lambda x: x[1]['CDLDOJI'] is not None, stocks.items()))
Output:
{'ABBA': {'company': 'Mahaka Media Tbk.', 'CDLDOJI': 'bullish'},
'ABMM': {'company': 'ABM Investama Tbk.', 'CDLDOJI': 'bullish'},
'ADMF': {'company': 'Adira Dinamika Multi Finance T', 'CDLDOJI': 'bullish'},
'ADRO': {'company': 'Adaro Energy Tbk.', 'CDLDOJI': 'bullish'},
'AGRS': {'company': 'Bank IBK Indonesia Tbk.', 'CDLDOJI': 'bullish'},
'AKKU': {'company': 'Anugerah Kagum Karya Utama Tbk', 'CDLDOJI': 'bullish'}}