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.
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]
some_list.append()
doesn’t return the list some_list
, it returns None
, that’s why
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.
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]
some_list.append()
doesn’t return the list some_list
, it returns None
, that’s why