Newbie: adding string values to a list?

bonono at gmail.com bonono at gmail.com
Wed Dec 21 10:24:44 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,
>
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.




More information about the Python-list mailing list