0

I have a python function that parses through financial data from the SEC which is in .json formatting.

The data structure for a particular part is as follows (adjusted for brevity):

{
  "facts":{
    ...
    {
      "shares":
      [
        ...
        {
          "end":"2022-10-14",
      "val":15908118000,
      "accn":"0000320193-22-000108",
      "fy":2022,
      "fp":"FY",
      "form":"10-K",
      "filed":"2022-10-28",
      "frame":"CY2022Q3I"
        },
    {
      "end":"2023-01-20",
      "val":15821946000,
      "accn":"0000320193-23-000006",
      "fy":2023,
      "fp":"Q1",
      "form":"10-Q",
      "filed":"2023-02-03",
      "frame":"CY2022Q4I"
    }
      ]
    }
  }
}

In order to get the value of the last list I do something like: shares_outstanding = data["facts"]...["shares"][-1]["val"]

The problem is I only want the last list that has a value of "10-K" for the form. How could I do this? I'm thinking maybe try to find all the lists that have "form": "10-K" and then select the last one but I'm not sure how to do this.

EDIT: I tried this method but it's quite messy and I fear it would be detrimental to performance to do this 100s of times for 100s of companies. Any suggestions:

for i in range(length):
if data["facts"]...["shares"][i]["form"] == "10-K":
    shares_outstanding.append(data["facts"]...["shares"][i]["val"])

print(shares_outstanding[-1])

La Myass
  • 29
  • 6
  • "I tried this method but it's quite messy and I fear it would be detrimental to performance to do this 100s of times for 100s of companies." Rather than appending everything and only choosing the last one, did you think of overwriting with the value that was just found? Rather than scanning the entire list forwards in order to find the last match (and not stopping at a match, in case there is another one), did you think of scanning backwards (and stopping if you find something)? – Karl Knechtel Mar 11 '23 at 05:30

0 Answers0