I'm trying to use format a string with a list of tuples, which has the following form:
formulas_and_labels = [('formula1', 'label1'),
('formula2', 'label2')]
This should be used to format the following string:
SQL_string = """SELECT
{} AS "{}",
{} AS "{}"
FROM
schema.table""".format(*formulas_and_labels)
I understand that this produces a IndexError: tuple index out of range
, because (*formulas_and_labels)
only contains two elements (tuples), whereas the string requires 4 elements (4x{}).
I managed to find a workaround by first unpacking the list using
formulas_and_labels_unpacked = [v for t in formulas_and_labels for v in t]
(found here).
However, I was wondering if a more direct way existed to directly "double-unpack" the list and tuples using something like .format(**formulas_and_labels)
or .format(*el) for el in formulas_and_labels)
?