python - Why does append return none in this code? -
this question has answer here:
- why list.append evaluate false? 5 answers
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
Post a Comment