0

this is actually a follow-up question which comes from this question.

The solution provided by Paul McGuire helped me reached to a better scenario where the data is in Pythonic format - 'nested list'. I have some experience working with BeautifulSoup before and looking at the nested list, I am wondering there might be a way to navigate the nested list like navigating through the HTML tree.

There are a few posts similar like mine but I figured out we have different expectations.

So my question is, say you have a nested list like this:

['DetailResult',
 ['status', ['Status', ['message', None], ['code', '0']]],
 ['searchArgument',
  ['DetailSearchArgument',
   ['reqPartNumber', 'BQ'],
   ['reqMfg', 'T'],
   ['reqCpn', None]]],
 ['detailsDto',
  [['DetailsDto',
    ['summaryDto',
     ['SummaryDto',
      ['PartNumber', 'BQ'],
      ['seMfg', 'T'],
      ['description', 'Fast']]],
    ['packageDto',
     [['PackageDto', ['fetName', 'a'], ['fetValue', 'b']],
      ['PackageDto', ['fetName', 'c'], ['fetValue', 'd']],
      ['PackageDto', ['fetName', 'd'], ['fetValue', 'z']],
      ['PackageDto', ['fetName', 'f'], ['fetValue', 'Sq']],
      ['PackageDto', ['fetName', 'g'], ['fetValue', 'p']]]],
    ['additionalDetailsDto',
     ['AdditionalDetailsDto',
      ['cr', None],
      ['pOptions', None],
      ['inv', None],
      ['pcns', None]]],
    ['partImageDto', None],
    ['riskDto',
     ['RiskDto',
      ['life', 'Low'],
      ['lStage', 'Mature'],
      ['yteol', '10'],
      ['Date', '2023']]],
    ['partOptionsDto',
     [['ReplacementDto',
       ['partNumber', 'BQ2'],
       ['manufacturer', 'T'],
       ['type', 'Reel']]]],
    ['inventoryDto',
     [['InventoryDto',
       ['distributor', 'V'],
       ['quantity', '88'],
       ['buyNowLink', 'https://www...']],
      ['InventoryDto',
       ['distributor', 'R'],
       ['quantity', '7'],
       ['buyNowLink', 'http://www.r.']],
      ['InventoryDto',
       ['distributor', 'RS'],
       ['quantity', '2'],
       ['buyNowLink', 'http://www.rs..']]]]]]]]

How can I get the elements whose "key" is PackageDto:

['PackageDto', ['fetName', 'a'], ['fetValue', 'b']],
['PackageDto', ['fetName', 'c'], ['fetValue', 'd']],
['PackageDto', ['fetName', 'd'], ['fetValue', 'z']],
['PackageDto', ['fetName', 'f'], ['fetValue', 'Sq']],
['PackageDto', ['fetName', 'g'], ['fetValue', 'p']]
Community
  • 1
  • 1
B.Mr.W.
  • 18,910
  • 35
  • 114
  • 178

1 Answers1

3

This should work:

def getelements(lst, key, res=None):
    if res is None:
        res = []
    for e in lst:
        if isinstance(e, list):
            if e[0] == key:
                res.append(e)
            else:
                getelements(e, key, res)
    return res

Test (l being your list):

res = getelements(l, 'PackageDto')
for n, e in enumerate(res):
    print(n, e)

yields

0 ['PackageDto', ['fetName', 'a'], ['fetValue', 'b']]
1 ['PackageDto', ['fetName', 'c'], ['fetValue', 'd']]
2 ['PackageDto', ['fetName', 'd'], ['fetValue', 'z']]
3 ['PackageDto', ['fetName', 'f'], ['fetValue', 'Sq']]
4 ['PackageDto', ['fetName', 'g'], ['fetValue', 'p']]
uselpa
  • 18,732
  • 2
  • 34
  • 52