On Wed, Mar 2, 2022 at 9:42 PM Avi Gross via Python-list <python-list@python.org> wrote: > > Larry, > > i waited patiently to see what others will write and perhaps see if you > explain better what you need. You seem to gleefully swat down anything > offered. So I am not tempted to engage.
But then you gave in to the temptation. > And it is hard to guess as it is not clear what you will do with this. In the interests of presenting a minimal example I clearly oversimplified. This is my use case: I get a dict from an outside source. The dict contains key/value pairs that I need to use to query a mongodb database. When the values in the dict are all scalar I can pass the dict directly into the query, e.g.: self._db_conn[collection_name].find(query). But if any of the values are lists that does not work. I need to query with something like the cross product of all the lists. It's not a true product since if a list is empty it means no filtering on that field, not no filtering on all the fields. Originally I did not know I could generate a single query that did that. So I was trying to come up with a way to generate a list of all the permutations and was going to issue a query for each individually. Clearly that would become very inefficient if the lists were long or there were a lot of lists. I then found that I could specify a list with the "$in" clause, hence my solution. > def query_lfixer(query): > for k, v in query.items(): > if type(v)==list: > query[k] = {"$in": v} > return query > > self._db_conn[collection_name].find(query_lfixer(query)) > > > So why did so many of us bother? Indeed - so why did you bother? -- https://mail.python.org/mailman/listinfo/python-list