Newbie: adding string values to a list?

P

planetthoughtful

Hi All,

Sorry for the influx of newbie questions -- I'm trying to figure these
things out on my own before bothering the community, but a lot of bits
and pieces are escaping me at the moment.

I'm retrieving a result set from an SQLite db (using the APSW module)
and I want to add the value from one of the fields in the result set to
a list. My current code looks something like:

result = []
for name in cursor.execute("SELECT name, address FROM contacts ORDER BY
name"):
result.extend(name)

print result

For reasons I (obviously) don't understand, the "name" values get
broken up into each individual letter of the values in the name field
in the result list.

So, if the table contained records:

Fred
Dave

When I "print result" I get:

['F','r','e','d','D','a','v','e']

What I'm looking for is:

['Fred','Dave']

Can anyone give me some advice on what I'm doing wrong?

Many thanks and much warmth,

planetthoughtful
 
D

Duncan Booth

planetthoughtful said:
result = []
for name in cursor.execute("SELECT name, address FROM contacts ORDER BY
name"):
result.extend(name)

print result

For reasons I (obviously) don't understand, the "name" values get
broken up into each individual letter of the values in the name field
in the result list.

Try interactive mode and it should be obvious:
result = []
result.extend('Fred')
result ['F', 'r', 'e', 'd']
result.append('Fred')
result ['F', 'r', 'e', 'd', 'Fred']
result.extend(['Fred'])
result ['F', 'r', 'e', 'd', 'Fred', 'Fred']
help(result.extend)
Help on built-in function extend:

extend(...)
L.extend(iterable) -- extend list by appending elements from the
iterable

in case it isn't obvious, the elements of a string are the individual
characters, so the extend method will append the individual characters. Use
the append method to put a single string on the end of a list.
 
T

Tim N. van der Leeuw

You need to use result.append(...) instead of result.extend(...)

(Been stumped with that myself too, several times, when I was still a
newby... Except was using the operator '+=' I think)

cheers,

--Tim
 
B

bonono

planetthoughtful said:
Hi All,

Sorry for the influx of newbie questions -- I'm trying to figure these
things out on my own before bothering the community, but a lot of bits
and pieces are escaping me at the moment.

I'm retrieving a result set from an SQLite db (using the APSW module)
and I want to add the value from one of the fields in the result set to
a list. My current code looks something like:

result = []
for name in cursor.execute("SELECT name, address FROM contacts ORDER BY
name"):
result.extend(name)

print result

For reasons I (obviously) don't understand, the "name" values get
broken up into each individual letter of the values in the name field
in the result list.

So, if the table contained records:

Fred
Dave

When I "print result" I get:

['F','r','e','d','D','a','v','e']

What I'm looking for is:

['Fred','Dave']

Can anyone give me some advice on what I'm doing wrong?

Many thanks and much warmth,
may be you can try result.append() instead of result.extend() and read
about their difference in the manual. but for this particular case you
may get what you want with list comprehension, or simply limit the
columns returned, as you are throwing away the addresses column anyway.
 
L

Larry Bates

planetthoughtful said:
Hi All,

Sorry for the influx of newbie questions -- I'm trying to figure these
things out on my own before bothering the community, but a lot of bits
and pieces are escaping me at the moment.

I'm retrieving a result set from an SQLite db (using the APSW module)
and I want to add the value from one of the fields in the result set to
a list. My current code looks something like:

result = []
for name in cursor.execute("SELECT name, address FROM contacts ORDER BY
name"):
result.extend(name)

print result

For reasons I (obviously) don't understand, the "name" values get
broken up into each individual letter of the values in the name field
in the result list.

So, if the table contained records:

Fred
Dave

When I "print result" I get:

['F','r','e','d','D','a','v','e']

What I'm looking for is:

['Fred','Dave']

Can anyone give me some advice on what I'm doing wrong?

Many thanks and much warmth,

planetthoughtful
You want to 'append' the string to the list. extend
is used to combine two lists. Since your string isn't
a list Python tries to help and converts it to one for
you. That's why you get the individual letters.
result.append(name)

-Larry Bates
 
P

planetthoughtful

Hi,

Thanks to all for your amazingly quick help! I'm learning much about
Python every day.

Much warmth,

planetthoughtful
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top