Difference between these two lists?
d at davea.name
Tue Jan 8 02:21:10 CET 2013
On 01/07/2013 08:00 PM, andydtaylor at gmail.com wrote:
> 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. [ANG, BAR, BPK, CTN, QGH, QHD, KXX]
> 2. ['ANG', 'BAR', 'BPK', 'CTN', 'QGH', 'QHD', 'KXX']
> cursor_from.execute('SELECT * FROM tubestations LIMIT 1000')
> stn_list_short = 
> for row in cursor_from:
> if row:
> # Station values for database
> 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.
More information about the Python-list