-1

I am using Python to retrieve a list of record data. This data has secondary record data inside of it. I come from Java background so to me it is a nested object.

Anyway to get a value from the nested object I currently do the following.

Acc = bankAccounts[0]
BAN = Acc['Bank_Details__r']
Records = BAN['records']
Fields = Records[0]
Id = Fields['Id']
print(Id)

While this works fine and all, I was wondering if there is a cleaner way to do it?

  • What’s up with those variable names? Can you share more of your program? It might be possible to change other areas in order to avoid this deeply nested access entirely. – AMC Jan 08 '20 at 16:14
  • 1
    `Id = bankAccounts[0]['Bank_Details__r']['records'][0]['Id']`? That's assuming the nested structure is *guaranteed*. If not, you will need to do additional handling on the appropriate levels. – r.ook Jan 08 '20 at 16:14
  • There is not much to the rest of the program except a query to get the bankAccounts list from my database. – Alexander Atkinsoon Jan 09 '20 at 08:38

1 Answers1

0

No, not really. You could make it all a single line (which I assume you know), or you can pretty easily subclass dict to enable dot notation access which you may consider cleaner.

Other than that, what you have is the best you can get.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154