Difference between these two lists?

Roy Smith roy at panix.com
Tue Jan 8 02:24:01 CET 2013


In article <700d2bd9-e1df-4d38-81c7-77029a36c47a at googlegroups.com>,
 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: 
> 
> Results:
> 1. [ANG, BAR, BPK, CTN, QGH, QHD, KXX]
> 2. ['ANG', 'BAR', 'BPK', 'CTN', 'QGH', 'QHD', 'KXX']
> 
> Code:
>    cursor_from.execute('SELECT * FROM tubestations LIMIT 1000')
>    
>    stn_list_short = []
>    for row in cursor_from:
>       if row[4]:
> 	# Station values for database
> 	stn_list_short.append(row[5])
> 
>    1. print stn_fields = '[%s]' % ', '.join(map(str, stn_list_short))  
>    2. print stn_list_short

Hi Andy,

You should try to reduce this down to some minimal test case.  In this 
case, the database code has nothing to do with it, it's purely a matter 
of how a list of strings is printed.

When you print a list, the repr() of each list element is printed.  The 
repr() of a string includes quotes.  For example:

>>> print str("foo")
foo
>>> print repr("foo")
'foo'

I'm not sure what "map(str, stn_list_short)" is all about.  I'm assuming 
stn_list_short is already a string, so that's a no-op.

In general, the best way to ask about code is to cut-and-paste the exact 
code that you ran.  You didn't run:

>    1. print stn_fields = '[%s]' % ', '.join(map(str, stn_list_short))  
>    2. print stn_list_short

because those are syntax errors.  I understand you were just trying to 
annotate your code to make it easier to explain.  A better way to do 
that would be to comment your code, something like:

>    print stn_fields = '[%s]' % ', '.join(map(str, stn_list_short))  # line 1
>    print stn_list_short  # line 2

Now you've got something which runs, and can be cut-and-pasted 
unmodified into your posting.  That reduces the chance of confusion.



More information about the Python-list mailing list