[Chicago] Making a list of strings

Brantley Harris deadwisdom at gmail.com
Mon Oct 19 20:45:25 CEST 2009


Right, so to clarify:

>>> y = [1, 2, 3]
>>> print y.append(4)
None

>>> print y
[1, 2, 3, 4]

Because *y.append()* acts upon the list *y*, but does not return a
value (or, technically, returns *None*).  This is to make sure you
don't think it returns a list with the value appended without acting
on the original list. (if this last part doesn't make sense, ignore
it)

On Mon, Oct 19, 2009 at 1:13 PM, Massimo Di Pierro
<mdipierro at cs.depaul.edu> wrote:
> because in
>
> y = list().append(whatever)
>
> you are bot storing the vaue returned by list() but the value returned by
> append and append does not return the list it acts on. append always returns
> None.
>
> On Oct 19, 2009, at 1:08 PM, Phil Robare wrote:
>
>> OK fellow pythonistas,  can anyone explain what is going on here?
>>
>>        I'm using a recent, but not most recent, version of Python.
>> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
>> (Intel)]
>>
>>        I want a list with a string value in it.  I make a sample string to
>> put in a list.
>> In [1]: s='string'
>>
>>        The obvious way gives me something different but expected
>> In [2]: list(s)
>> Out[2]: ['s', 't', 'r', 'i', 'n', 'g']
>>
>>        So I try appending something to a newly created empty list
>> In [3]: y=list().append(s)
>>
>>        And I get a null value.  This is unexpected,
>> In [4]: y
>>
>>        since doing the steps independantly gives me what I want.
>> In [5]: y=list()
>> In [6]: y.append(s)
>> In [7]: y
>> Out[7]: ['string']
>>
>>
>> I want to pass this as a parameter and it seems non-pythonic to have
>> to create a variable and then pass it when all I want is a single use.
>> Any ideas for a better work-around?
>>
>> Phil
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list