By Python convention, all mutating functions return None
. Nonmutating functions return the new value. insert
is a mutating function (changes the object it operates on), so it returns None
; you then assign it to c
.
In fact, there is no way to do this in one statement in current Python. In the future (almost certainly in Python 3.8), there is a proposal for a walrus operator that will allow you to shorten this:
(c := ['545646', 'text_item', '151561']).insert(1, '555')
though I believe Pythonistas will frown on it :)
EDIT: With the question in the comments, how to do an insert as an expression? The easiest way is to define another function; for example:
def insert_and_return_list(lst, pos, val):
lst.insert(pos, val)
return lst
c = insert_and_return_list(['545646', 'text_item', '151561'], 1, '555')
You could also avoid insert
altogether, and use slices and splats:
[*lst[:1], '555', *lst[2:]]