How to extend a tuple of tuples?
Frank Millman
frank at chagford.com
Fri Sep 9 08:21:14 EDT 2016
"Ned Batchelder" wrote in message
news:44e067ce-f499-4ca8-87bd-94b18dfc0572 at googlegroups.com...
On Friday, September 9, 2016 at 6:13:37 AM UTC-4, Frank Millman wrote:
> > "Frank Millman" wrote in message news:nqtlue$unj$1 at blaine.gmane.org...
> >
> > The one I was looking for was
> >
> > a += (5, 6),
> >
> > I understand it now - makes perfect sense.
>
> I'm curious why you are using a growing tuple, instead of a list.
> This seems like a much more natural use for a list.
>
The short answer is that I am using it as a dictionary key.
However, from The Zen of Python -
If the implementation is hard to explain, it's a bad idea.
Let's see if I can explain.
I am building a series of JOINs for a SQL statement. Some of them can be
nested - table A is joined from table B is joined from table C. In other
parts of the same SQL statement, table A could be joined from table D which
is joined from table E.
For each potential candidate, I need to check if I have already built the
JOIN. If so, just use it. If not, build it, and update a dictionary to
indicate that it has been built.
The key to the dictionary is the path to the joined table, which could be
several levels deep. I build up a tuple to represent the path. Each table
needs two pieces of information to identify it, so I use a tuple for each
identifier.
The value portion of the dictionary is simply the alias generated for the
joined table. I use the alias in the body of the SQL statement.
I don't know if that explains it very well. I can supply more info if anyone
is curious, or wishes to suggest a better approach.
Frank
More information about the Python-list
mailing list