Home > database >  How to get specific values from a nested list in Python
How to get specific values from a nested list in Python

Time:02-06

Could you help me getting the specific values I wanted in the below list

list=[['Russia',[{'id': 250282,'d_id': 19553,'p_id': 1796,'value': 'silver'},
                {'id': 250212,'d_id': 19553,'p_id': 1896,'value': 'gold'},
                {'id': 250242,'d_id': 19553,'p_id': 1396,'value': 'iron'},
                {'id': 250082,'d_id': 19553,'p_id': 1496,'value': 'cobalt'}]],
      ['China',[{'id': 210282,'d_id': 193,'p_id': 1196,'value': 'silver'},
                {'id': 220212,'d_id': 193,'p_id': 1396,'value': 'iron'},
                {'id': 240242,'d_id': 193,'p_id': 1586,'value': 'iron'},
                {'id': 250082,'d_id': 193,'p_id': 1492,'value': 'gold'}]],
      ['Africa',[]],
      ['USA',[{'id': 200282,'d_id': 5531,'p_id': 1093,'value': 'iron'},
                {'id': 253212,'d_id': 5531,'p_id': 1843,'value': 'gold'},
                {'id': 255242,'d_id': 5531,'p_id': 1323,'value': 'iron'},
                {'id': 257082,'d_id': 5531,'p_id': 1409,'value': 'cobalt'}]],
      ['UK',[]]]


output should be:
  'Russia', 19553
  'China',  193
  'Africa', 0  
  'USA',    5531
  'UK',     0

I am trying to get countries and unique values of d_id because it will be the same for all records and impute missing values with 0

I tried for loops and slicing of lists but nothing worked out If anyone of you have a solution for this that would be much appreciated.

output should be: 'Russia', 19553 'China', 193 'Africa', 0
'USA', 5531 'UK', 0

In the above output Africa and UK d_id values are imputed with 0

CodePudding user response:

You should post the code you've tried in your question next time.

{l[0]: set([_d['d_id'] for _d in l[1]]) if len(l[1]) > 0 else set([0]) for l in your_list}

CodePudding user response:

Your inner lists have 2 values - a country name and a list of records. You could iterate the list, using tuple expansion to get those two values. If the list of records is not empty, grab the first value, otherwise use zero.

list=[['Russia',[{'id': 250282,'d_id': 19553,'p_id': 1796,'value': 'silver'},
                {'id': 250212,'d_id': 19553,'p_id': 1896,'value': 'gold'},
                {'id': 250242,'d_id': 19553,'p_id': 1396,'value': 'iron'},
                {'id': 250082,'d_id': 19553,'p_id': 1496,'value': 'cobalt'}]],
      ['China',[{'id': 210282,'d_id': 193,'p_id': 1196,'value': 'silver'},
                {'id': 220212,'d_id': 193,'p_id': 1396,'value': 'iron'},
                {'id': 240242,'d_id': 193,'p_id': 1586,'value': 'iron'},
                {'id': 250082,'d_id': 193,'p_id': 1492,'value': 'gold'}]],
      ['Africa',[]],
      ['USA',[{'id': 200282,'d_id': 5531,'p_id': 1093,'value': 'iron'},
                {'id': 253212,'d_id': 5531,'p_id': 1843,'value': 'gold'},
                {'id': 255242,'d_id': 5531,'p_id': 1323,'value': 'iron'},
                {'id': 257082,'d_id': 5531,'p_id': 1409,'value': 'cobalt'}]],
      ['UK',[]]]


output = []
for name, records in list:
    if records:
        d_id = records[0]['d_id']
    else:
        d_id = 0
    output.append((name, d_id))

for name, d_id in output:
    print(f"    '{name}': {d_id}")
  • Related