1

Maybe this has been asked already, but I can't find it, and I need help. I have a list of tuple pairs:

mylist = [('name1', 'name2'), ('name3', 'name4'), ('name5','name6')]

The list is generated from a MySQL database and can be of any length. It is possible that some of the names are the same. It is not absolutely necessary that the data type be a list of tuples. That's just what made the most sense to me. It can be a list of lists, tuple of tuples, etc. A dictionary doesn't make sense to me, because, as previously stated, any of the names can be the same. This is in python using django, which can't be changed.

Anyway, I want to send an email with the contents of Mylist as the body. I want to format it, though, so that it looks in the body of the email like this:

name1, name2
name3, name4
name5, name6

In other words, I want it to look like normal text you would see in an email body. Initially, my thinking was that I had to use a function and generate a variable for each line of output. There may be a way to get this done without creating new variables for each newline, but I can't figure it out. To be honest, I haven't figured it out at all. I've tried for loops, while loops, recursive functions, etc. No luck.

This is for work, and I could really use some help. Thanks

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
pyetti
  • 188
  • 2
  • 14

1 Answers1

0

The most Pythonic way that comes to mind would be:

body = '\n'.join('%s, %s' % pair for pair in mylist)
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • That makes sense, but for some reason \n isn't giving me a newline. It's being appended to the second item in the tuple: name1, name2\n... – pyetti Sep 27 '13 at 23:30
  • 1
    Nevermind. It works in the email body perfectly. You nailed it. Thank you so much. You saved my Friday night. Beers on me! – pyetti Sep 27 '13 at 23:39