If I understand your question correctly, this is the format string you want:
"{:<{}} " * len(list_var)
This of course requires, in your particular case, that your lists (d_max
and list_var
) are both the correct length. But it'll work for any length.
To make the actual use case a little cleaner, I'd probably put the list of arguments together in a separate statement. The general case for your loop could look something like this:
args = []
for i in range(len(list_var)): # Build list of value/width arguments
args += [list_var[i], d_max[i]]
print(("{:<{}} " * len(list_var)).format(*args))
Or if you're a stickler for one-liners, you could use this monstrosity (does the same thing as the explicit loop above, except that it's much harder to read.):
# Ew
print(("{:<{}} " * len(list_var)).format(*functools.reduce(
lambda a, i: a + [list_var[i], d_max[i]],
range(len(list_var)),
[])))
Finally, as others have noted, you shouldn't rely on a dictionary to preserve order. If it's important that the items are presented in order by their dictionary keys, you should make sure to sort them before you iterate:
for k,v in sorted(d.items()):