Difference between these two lists?

Dave Angel d at davea.name
Mon Jan 7 20:21:10 EST 2013


On 01/07/2013 08: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: 
>
> 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


#2 is easy.  it's just the standard way a list prints itself.  It puts
brackets at begin and end, and then calls repr() on each of the
elements.  Since those elements are str objects, each gets quotes around
it.  Then the list logic adds commas and puts it all together.

In #1, you're doing it by hand.  If you wanted the same result, you'd
have to map the repr, not the str value of each item.

(untested)

.join(map(repr, stn_list_short)



--
DaveA



More information about the Python-list mailing list