Difference between these two lists?

A

andydtaylor

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

Thanks!

Andy
 
A

andydtaylor

I think I can answer my own question on reflection.... the first one is actually a string I think? I was confused by the square brackets around the placeholder %s.
 
C

Chris Angelico

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
 
C

Chris Angelico

I think I can answer my own question on reflection.... the first one is actually a string I think? I was confused by the square brackets around the placeholder %s.

That's correct. Your first line is putting square brackets around a
comma-joined list of strings; the second gives the representation of a
list. They happen to be superficially similar.

ChrisA
 
D

Dave Angel

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)
 
R

Roy Smith

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:
'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.
 
A

andydtaylor

Thanks, I think I'm clear now.

I guess (map(str, stn_list)) was all about how to make a string starting with integers. I picked that up and began using it without realising it was over catering for a list already containing strings, and join(stn_list) was really all I required.

Repr and Eval I think I get. Eval certainly. That's a familiar concept, and one I hope to use tomorrow to feed a line to psycopg2.

I might have opened a can of worms on map and repr though... I'll do some more reading tomorrow.

Top tip on the code annotation, I didn't really think that through.

Thanks again,

Andy
 
C

Chris Angelico

Repr and Eval I think I get. Eval certainly. That's a familiar concept, and one I hope to use tomorrow to feed a line to psycopg2.

I hope not. Why do you need eval? It's extremely dangerous.

Chances are there's a better way to do it; if your users are entering
strings like

"['Foo', 'Bar']"

and you want them to be interpreted as lists, then you can get a much
safer function ast.literal_eval - it's equivalent to eval, but (as the
name suggests) works only with literals, so you can't call functions
etc. But even that may be overkill, depending on what you actually
need.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top