iA


Turn a python list into a human-readable variable

by Andy Boyle.

Had a problem today where I needed to turn a Python list called email_contents into a variable that looked pretty. Basically, I needed this:

['blah1','blah2','blah'3]

To look like this:

"blah

blah2

blah3"

I wrote a send_error_email function for a script, which I will explain in a later post, that needs to turn that list into something more human readable. The solution? .join()

Here’s what I ended up doing:

msg = "\n\n".join(email_contents)

Voila. It’s now pretty, with a line separating each item in the list.