python - Why does append return none in this code? -


this question has answer here:

list = [1, 2, 3] print list.append(4)   ## no, not work, append() returns none ## correct pattern: list.append(4) print list  ## [1, 2, 3, 4] 

i'm learning python , i'm not sure if problem specific language , how append implemented in python.

append destructive operation (it modifies list in place instead of of returning new list). idiomatic way non-destructive equivalent of append be

l = [1,2,3] print l + [4] # [1,2,3,4] print l # [1,2,3] 

to answer question, guess if append returned newly modified list, users might think non-destructive, ie might write code like

m = l.append("a") n = l.append("b") 

and expect n [1,2,3,"b"]


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -