Difference between these two lists?

Chris Angelico rosuav at gmail.com
Tue Jan 8 02:11:36 CET 2013


On Tue, Jan 8, 2013 at 12:00 PM,  <andydtaylor at gmail.com> wrote:
> Hi,
>
> Python newbie here again - this is probably a quick one. What's the difference between the lines I've numbered 1. and 2. below, which produce the following results:
>    1. print stn_fields = '[%s]' % ', '.join(map(str, stn_list_short))
>    2. print stn_list_short

Your first line explicitly joins the strings with commas; the second
implicitly calls repr() on the whole list, which does its best to
produce a valid Python literal. Check out the docs on repr() for the
details on that; you'll see different results depending on the content
of the strings, but at very least they'll always be quoted.

ChrisA



More information about the Python-list mailing list