why I can't append directly after a split in python

Question:

test = "123213 32543543 52354 234 34"
a = test.split().append("sd")
print (a)

The above code will give me a None in output, while the following code will output a list:

test = "123213 32543543 52354 234 34"
a = test.split()
a.append("sd")
print (a)

Can anyone explain it? Thanks.

Asked By: PunyTitan

||

Answers:

This is because .append() list operation returns None.

In [1]: list1 = [1,2,3,4] # some list

In [2]: a = list1.append(5) # append '5' to the list and assign return value to 'a'

In [3]: print a
None # means '.append()' operation returned None

In [4]: list1 
Out[4]: [1, 2, 3, 4, 5]

In [5]: list1.append(6) 

In [6]: list1
Out[6]: [1, 2, 3, 4, 5, 6]
Answered By: Rahul Gupta

some_list.append() doesn’t return the list some_list, it returns None, that’s why

Answered By: bakkal
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.