Newbie: adding string values to a list?

Larry Bates larry.bates at websafe.com
Wed Dec 21 10:32:41 EST 2005


planetthoughtful wrote:
> 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



More information about the Python-list mailing list