0
list1 = ["a", "b", "c"]
list2 = list1.append("d")
print(list2)

this output None.Don't know why?

list3 = ["a", "b", "c"]
list3.append("d")
list4 = list3
print(list4)

this correct and output ['a', 'b', 'c', 'd']

  • 1
    You don't need `list4`. If you read the documentation for the append function, it clearly explains what the function returns, and it's not another list. Perhaps you want to `+ ["d"]` ? – OneCricketeer Jan 22 '22 at 06:41
  • `append` wont return new list because it is `inplace` operation – Fareed Khan Jan 22 '22 at 06:41
  • Welcome to Stack Overflow. This is [very commonly asked about](https://duckduckgo.com/?q=python+list.append+returns+none+site%3Astackoverflow.com). It is a [good idea to try to do research first](https://meta.stackoverflow.com/questions/261592). – Karl Knechtel Jan 22 '22 at 07:07

1 Answers1

0

You have to do it this way:

list1 = ["a", "b", "c"]
list1.append("d")
list2 = list1
print(list2)

This answer explains why very well:
https://stackoverflow.com/questions/20016802/why-does-list-append-return-none/#answer-20016976
  • Welcome to Stack Overflow. Please read [answer]. If a question is properly answered in another question already on the site, then it is a *duplicate* and should not be answered. Instead, vote to close the question using the `Close` link under the question. (It has now already been closed, but you should know this for the future.) – Karl Knechtel Jan 22 '22 at 07:08
  • Great! Thanks for this. – chengxuyuan9527 Jan 22 '22 at 07:15