[M Siddharth Prajosh <spraj...@gmail.com>] > This is more of a doubt than a new idea. Python has always worked > intuitively but this was a bummer. > > A list has an append method. So I can do list.append(value). > I tried doing list(range(10)).append(10) and it returns None.
Yes. Most methods that mutate an object return None. > I'd usually assume list(range(10)) returns a list, It does. > to which I can append whatever I want. And you can. > I tried this with List comprehension also. Doesn't work there either. > Why doesn't this work? It does work :-) list(range(10)) created an anonymous list, then .append(10) appended 10 to that anonymous list, and list.append _always_ returns None. Since the list wasn't bound to any name, it became trash (unreachable garbage) as soon as the .append() ended, so the list was thrown away. Give it a name for clarity: >>> xs = list(range(10)) >>> print(xs.append(10)) # demonstrating that it does return None None >>> xs # and the append worked fine [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] > Moreover, shouldn't it work? > How do I add that feature in Python? Better instead to learn how Python works here - it's not broken :-) _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/FSTS5AIWZN4OIXOBKQGORTM2ZI3FTBNV/ Code of Conduct: http://python.org/psf/codeofconduct/