Re: Newbie: adding string values to a list?



planetthoughtful wrote:

> 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.
.



Relevant Pages

  • How to append existing series?
    ... I want to append specific series on a chart. ... I realize using the extend method would do the trick, ... I want to be able to extend specific series so SeriesCollection.Extend, ...
    (microsoft.public.excel.charting)
  • Re: Append text to top of file with VB 6.0?
    ... You can read and write, extend, and truncate, and that's it. ... "Ron" wrote in message ... I need to append one full line with a CR LF and sometimes ...
    (microsoft.public.vb.winapi)
  • Re: Inconsistent reaction to extend
    ... Extend has deliberate side-effects: ... Imagine you have a list of 100,000 large objects, and ... The way Python works is that append ... The extend method could just have returned self. ...
    (comp.lang.python)
  • Re: list-like behaviour of etree.Element
    ... list-like methods: append, insert, remove. ... Any special reason why it ... doesn't support pop and extend? ...
    (comp.lang.python)
  • Re: How to append existing series?
    ... Or you can use named formulas for series a la ... Dynamic Charts ... > I want to append specific series on a chart. ... > I want to be able to extend specific series so SeriesCollection.Extend, ...
    (microsoft.public.excel.charting)