Now how do I search if 'column1' has the value "11"
any(arow['column1'] == 'value11' for arow in table.iteritems())
Is this method of generating tables wrong?
No, itβs just very βexposedβ, perhaps too much - it could be encapsulated in a class that provides the methods you need, then the question of how to best implement them does not affect the rest of your application.
Is there a better way to implement such tables with an easier search?
Once you have developed a class whose interface you would like to use, you can experiment with very different implementation approaches and compare them with the workload representing your usage pattern so that you can find out what is best for you (provided that table processing and searching are a large part of the runtime of your application, of course - to find out the profile of your application).
I had similar, but not identical needs for the large internal application that I support at work, except that the row indices are integer (only the column names are strings), the order of the columns is important, and the workload is more about editing table (add, delete, reorder rows or columns, rename columns, etc.). I started with a table that reveals the functionality I need, with a simple rough and ready implementation inside (a list of dicts, as well as a list of column names to organize the columns); and so far I have developed it (regardless of the actual parts of the "application level", but based on profiling and comparative analysis) for completely different implementations (currently based on numpy ).
I think that you should continue similar lines: "dress" your current implementation in a nice "interface" with all the necessary methods, profile the application - if this table object is not a performance bottleneck, everything is ready; if this is a bottleneck, you can optimize the implementation (experiment, measurement, repeat ;-) without violating any of the other applications.
Inheriting from dict not a good idea, because you probably don't want to reveal all the functionality of dict rich; plus what are you doing, roughly speaking, an inefficient implementation of collections.defaultdict(dict) . So, encapsulate the latter:
import collections class Table(object): def __init__(self): self.d = collections.defaultdict(dict) def add(self, row, col, val): self.d[row][col] = val def get(self, row, col, default=None): return self.d[row].get(col, default) def inrow(self, row, col): return col in self.d[row] def incol(self, col, val): return any(x[col]==val for x in self.d.iteritems())
etc. etc. - write all the methods that your application needs with useful short names, and then maybe see if you can use some of them as special methods if they are often used in this way, for example, maybe (assuming Python 2. * - requires a slightly different syntax in 3. *):
def __setitem__(self, (row, col), val): self.add(row, col, val)
etc. After you work with the code, the time comes for profiling, benchmarking and, possibly, internal optimization of the implementation.