Big-O of iterating through nested structure While trying to understand complexity I run into an example of going through records organized in following way:
data = [
  {&quot;name&quot;: &quot;category1&quot;, &quot;files&quot;: [{&quot;name&quot;: &quot;file1&quot;}, {&quot;name&quot;: &quot;file2&quot;}],
  {&quot;name&quot;: &quot;category2&quot;, &quot;files&quot;: [{&quot;name&quot;: &quot;file3&quot;}]
]

The task requires to go through all file records which is straight forward:
for category in data:
  for file in category[&quot;files&quot;]:
    pass

It seems like complexity of this algorithm is O(n * m), where n is length of data and m is max length of files array in any of data records. But is O(n * m) only correct answer?
Because even there are two for-loops it still looks like iterating over a global array of file records organized in nested way. Is it legit to compare with iteration over different structure like that:
data = [
  ('category1', 'file1'),
  ('category1', 'file2'),
  ('category2', 'file3'),
]
for category, file in data:
  pass

...where complexity is obviously O(n), and n is a total number of records?
A better bound on the complexity is $O(k)$, where $k$ is the total number of files.
A similar situation is encountered in algorithms for sparse matrices. Sparse matrices are stored as a list of all non-zero entries. The running time is often analyzed in term of the number of non-zero entries rather than the dimensions of the matrix. The reason is that matrices encountered in practice are often sparse (or can be sparsified), and we are interested in algorithms taking advantage of this.