Home > Software design >  How to select some key values from a dictionary and assign to another dictionary in python
How to select some key values from a dictionary and assign to another dictionary in python

Time:01-13

I have a variable which stores below dictionary

initial_ltp =
{'s': 'ok',
 'd': [{'n': 'MCX:CRUDEOIL23JANFUT',
   's': 'ok',
   'v': {'ch': 47.0,
    'chp': 0.74,
    'lp': 6377.0,
    'spread': 2.0,
    'ask': 6379.0,
    'bid': 6377.0,
    'open_price': 6330.0,
    'high_price': 6393.0,
    'low_price': 6305.0,
    'prev_close_price': 6330.0,
    'volume': 8410,
    'short_name': 'CRUDEOIL23JANFUT',
    'exchange': 'MCX',
    'description': 'MCX:CRUDEOIL23JANFUT',
    'original_name': 'MCX:CRUDEOIL23JANFUT',
    'symbol': 'MCX:CRUDEOIL23JANFUT',
    'fyToken': '1120230119244999',
    'tt': 1673481600,
    'cmd': {'t': 1673518200,
     'o': 6376.0,
     'h': 6378.0,
     'l': 6375.0,
     'c': 6377.0,
     'v': 19,
     'tf': '15:40'}}},
  {'n': 'MCX:SILVERMIC23FEBFUT',
   's': 'ok',
   'v': {'ch': 485.0,
    'chp': 0.71,
    'lp': 68543.0,
    'spread': 5.0,
    'ask': 68545.0,
    'bid': 68540.0,
    'open_price': 68200.0,
    'high_price': 68689.0,
    'low_price': 68200.0,
    'prev_close_price': 68058.0,
    'volume': 49595,
    'short_name': 'SILVERMIC23FEBFUT',
    'exchange': 'MCX',
    'description': 'MCX:SILVERMIC23FEBFUT',
    'original_name': 'MCX:SILVERMIC23FEBFUT',
    'symbol': 'MCX:SILVERMIC23FEBFUT',
    'fyToken': '1120230228242738',
    'tt': 1673481600,
    'cmd': {'t': 1673518200,
     'o': 68525.0,
     'h': 68543.0,
     'l': 68524.0,
     'c': 68543.0,
     'v': 140,
     'tf': '15:40'}}}]} 

I am trying to collect ('n') and ('lp') and save it in a different dictionary using code:

if 'd' in initial_ltp.keys():
    ltp[initial_ltp['d'][0]['n']] = initial_ltp['d'][0]['v']['lp']

But it is only taking the first n and lp

ltp
{'MCX:CRUDEOIL23JANFUT': 6377.0}

My expected output:

ltp
{'MCX:CRUDEOIL23JANFUT': 6377.0, 'MCX:SILVERMIC23FEBFUT': 68543.0}

How can I get both the values

CodePudding user response:

You have to loop over the list. Using ltp[initial_ltp['d'][0] will just extract for the first element of the list.

Here is an example:

results = {}
for doc in initial_ltp["d"]:
    results[doc["n"]] = doc["v"]["lp"]

print(results)

CodePudding user response:

Use the following approach with dict comprehension:

ltp = {d['n']:d['v']['lp'] for d in initial_ltp['d']} if 'd' in initial_ltp else {}

{'MCX:CRUDEOIL23JANFUT': 6377.0, 'MCX:SILVERMIC23FEBFUT': 68543.0}

CodePudding user response:

when you use the "=" operator, it replaces your value in the dictionary key. you want to add keys to your dictionary so I suggest using this:

if 'd' in initial_ltp.keys():
    for o in initial_ltp['d']:
       if n in o:
          ltp[initial_ltp['d'][0]['n']].append(initial_ltp['d'][0]['v']

['lp']

CodePudding user response:

It's because you selected only first item in the 'd'.

try a loop like this:

ltp={}
if 'd' in initial_ltp.keys():
    for i in range(len(initial_ltp['d'])):
        ltp[initial_ltp['d'][i]['n']] = initial_ltp['d'][i]['v']['lp']
print (ltp)

Output:

{'MCX:CRUDEOIL23JANFUT': 6377.0, 'MCX:SILVERMIC23FEBFUT': 68543.0}
  • Related