smartest way to join two lists into a formatted string

This implementation is, on my system, faster than either of your two functions and still more compact.

c=", ".join('%s=%s' % t for t in zip(a, b))

Thanks to @JBernardo for the suggested improvement.

In more recent syntax, str.format is more appropriate:

c=", ".join('{}={}'.format(*t) for t in zip(a, b))

This produces the largely the same output, though it can accept any object with a __str__ method, so two lists of integers could still work here.

Leave a Comment