0

I have this kind of 50 dictionary means that I have over 50 kind of dictionary for different values. like symbol and other key value.

I want to get only symbol and open values in a list.

{'pricebandupper': 380.65, 'symbol': 'ADANIPORTS', 'applicableMargin': 40.0, 'bcEndDate': '26-JUN-20', 'totalSellQuantity': None, 'adhocMargin': 18.19, 'companyName': 'Adani Ports and Special Economic Zone Limited', 'marketType': 'N', 'exDate': '17-JUN-20', 'bcStartDate': '19-JUN-20', 'css_status_desc': 'Listed', 'dayHigh': 349.75, 'basePrice': 346.05, 'securityVar': 18.31, 'pricebandlower': 311.45, 'sellQuantity5': None, 'sellQuantity4': None, 'sellQuantity3': None, 'cm_adj_high_dt': '10-JUN-19', 'sellQuantity2': None, 'dayLow': 340.4, 'sellQuantity1': None, 'quantityTraded': 2095819.0, 'pChange': 0.69, 'totalTradedValue': 8017.47, 'deliveryToTradedQuantity': 21.32, 'totalBuyQuantity': 2730.0, 'averagePrice': 344.84, 'indexVar': None, 'cm_ffm': 26717.33, 'purpose': 'ANNUAL GENERAL MEETING', 'buyPrice2': None, 'secDate': '10-Jun-2020 15:00:00', 'buyPrice1': 346.55, 'high52': 429.5, 'previousClose': 346.05, 'ndEndDate': None, 'low52': 203.0, 'buyPrice4': None, 'buyPrice3': None, 'recordDate': None, 'deliveryQuantity': 446896.0, 'buyPrice5': None, 'priceBand': 'No Band', 'extremeLossMargin': 3.5, 'cm_adj_low_dt': '23-MAR-20', 'varMargin': 18.31, 'sellPrice1': None, 'sellPrice2': None, 'totalTradedVolume': 2324983.0, 'sellPrice3': None, 'sellPrice4': None, 'sellPrice5': None, 'change': 2.4, 'surv_indicator': None, 'ndStartDate': None, 'buyQuantity4': None, 'isExDateFlag': False, 'buyQuantity3': None, 'buyQuantity2': None, 'buyQuantity1': 2730.0, 'series': 'EQ', 'faceValue': 2.0, 'buyQuantity5': None, 'closePrice': 0.0, 'open': 346.05, 'isinCode': 'INE742F01042', 'lastPrice': 348.45}

What I have tried:

symbol = data['symbol']
open = data['open']

Here data is the dictionary as mentioned above. please help. here is an example of several dictionaries.

Sandeep Sharma
  • 639
  • 2
  • 9
  • 34

3 Answers3

1

(I have updated this answer, and assumed the dictionaries are stored in an array).

# So you a file with list having 50 dictionaries like this
[ {}, {}, {}, {}, {} ]

To solve this, we will just traverse through the array, and to get an index, and then obtain the value from each index of list of dictionaries:

'''
Assuming the list goes like this
list_data = [{}, {}, {}, {}] having you dictionary only, I am just showing it 
for the sake of understanding
'''
list_data = [{}, {}, {} ....]
new_list = [] # where you want to store the result

# traversing through each dict item in list_data
# dictionary.get(key) is very famously used for getting exact key's value
# Read more about dictionaries here: 
# https://www.w3schools.com/python/python_dictionaries.asp
for i in range(len(list_data)):
    #assuming that every dictionary has the key 'symbol' and 'open'
    new_list.append([list_data[i].get('symbol'), list_data[i].get('open')])

print(new_list)

# Output will be consisting the array of array, like this
# [ ['ADANIPORTS', 346.05], .... ] since every dictionary should show up it's 
# symbol and open value uniquely
# ..... => This is for more values
>>> [['ADANIPORTS', 346.05], ..... ]
halfer
  • 19,824
  • 17
  • 99
  • 186
Alok
  • 8,452
  • 13
  • 55
  • 93
  • Thanks for your answer. actually it only prints `'ADANIPORTS', 346.05` but I have 50 these kinds of a dictionary as mentioned in the question. here is all dictionary.https://pastebin.com/C8w077Nr – Sandeep Sharma Jun 10 '20 at 14:16
  • So you want to go through each one of the dictionary, and print the values of `symbol` and `price` out of them? Am I getting your requirement right @SandeepSharma? – Alok Jun 10 '20 at 14:18
  • Is it possible for you to store those dictionaries in `[]`, if yes, then tell me asap, I will give you the solution. Or store it into the list or `[]` and let me know, I will give out the solution then @SandeepSharma – Alok Jun 10 '20 at 14:20
  • Yeah, so I am assuming that you can store your 50 dictionaries in lsit, and I am giving out the solution for the same @SandeepSharma – Alok Jun 10 '20 at 14:22
  • yeah. like dictionary only contains `symbol` and for other `open`. – Sandeep Sharma Jun 10 '20 at 14:24
  • Hey @SandeepSharma, check out my new solution, and let me know if that helps – Alok Jun 10 '20 at 14:39
  • Happy to help @SandeepSharma. :) – Alok Jun 10 '20 at 17:23
0

you can try this not sure that what you want:

data = {'pricebandupper': 380.65, 'symbol': 'ADANIPORTS', 'applicableMargin': 40.0, 'bcEndDate': '26-JUN-20', 'totalSellQuantity': None, 'adhocMargin': 18.19, 'companyName': 'Adani Ports and Special Economic Zone Limited', 'marketType': 'N', 'exDate': '17-JUN-20', 'bcStartDate': '19-JUN-20', 'css_status_desc': 'Listed', 'dayHigh': 349.75, 'basePrice': 346.05, 'securityVar': 18.31, 'pricebandlower': 311.45, 'sellQuantity5': None, 'sellQuantity4': None, 'sellQuantity3': None, 'cm_adj_high_dt': '10-JUN-19', 'sellQuantity2': None, 'dayLow': 340.4, 'sellQuantity1': None, 'quantityTraded': 2095819.0, 'pChange': 0.69, 'totalTradedValue': 8017.47, 'deliveryToTradedQuantity': 21.32, 'totalBuyQuantity': 2730.0, 'averagePrice': 344.84, 'indexVar': None, 'cm_ffm': 26717.33, 'purpose': 'ANNUAL GENERAL MEETING', 'buyPrice2': None, 'secDate': '10-Jun-2020 15:00:00', 'buyPrice1': 346.55, 'high52': 429.5, 'previousClose': 346.05, 'ndEndDate': None, 'low52': 203.0, 'buyPrice4': None, 'buyPrice3': None, 'recordDate': None, 'deliveryQuantity': 446896.0, 'buyPrice5': None, 'priceBand': 'No Band', 'extremeLossMargin': 3.5, 'cm_adj_low_dt': '23-MAR-20', 'varMargin': 18.31, 'sellPrice1': None, 'sellPrice2': None, 'totalTradedVolume': 2324983.0, 'sellPrice3': None, 'sellPrice4': None, 'sellPrice5': None, 'change': 2.4, 'surv_indicator': None, 'ndStartDate': None, 'buyQuantity4': None, 'isExDateFlag': False, 'buyQuantity3': None, 'buyQuantity2': None, 'buyQuantity1': 2730.0, 'series': 'EQ', 'faceValue': 2.0, 'buyQuantity5': None, 'closePrice': 0.0, 'open': 346.05, 'isinCode': 'INE742F01042', 'lastPrice': 348.45}

result = []

result.append(data['symbol'])
result.append(data['open'])

print(result)
0

Although you haven't specified exact format you want the output to be in. Assuming you want all the value of certain keys in a list, here's the example code you can use.

inp_dict = {1:2,3:4} keys = [1,3] output_lis = [a.get(i) for i in b]

dictionary[key] is also used, but 'get' built-in function allows you to provide a default value if the key is missing.

dictionary.get(key[, default])

Pooja Sonkar
  • 132
  • 10