[Tutor] assignment question
John Fouhy
john at fouhy.net
Mon Nov 12 05:30:43 CET 2007
On 12/11/2007, Ryan Hughes <ryan251 at gmail.com> wrote:
> Why does the following not return [1,2,3,4] ?
>
> >>> x = [1,2,3].append(4)
> >>> print x
> None
List methods like .append() and .sort() modify lists in-place, as
opposed to creating new lists. To remind you of this, those methods
return None instead of returning the modified list.
Otherwise (if .append() returned the modified list), you might be
tempted to write code like this:
x = getSomeList()
y = x.append(3)
and forget that x and y are the same list.
If you want to join two lists together to make a new one, you can use +:
>>> x = [1, 2, 3] + [4]
>>> x
[1, 2, 3, 4]
Hope this helps,
--
John.
More information about the Tutor
mailing list