I have something similar to:
>>> S=list()
>>> T=[1,2,3]
>>> for t in T:
... print(S.append(t))
The output I am getting is:
...
None
None
None
I expect S contains t. Why this is not working with me ?
list.append() does not return anything. Because it does not return anything, it default to None (that is why when you try print the values, you get None).
It simply appends the item to the given list in place. Observe:
>>> S = list()
>>> T = [1,2,3]
>>> for t in T:
... S.append(t)
>>> print(S)
[1, 2, 3]
Another example:
>>> A = []
>>> for i in [1, 2, 3]:
... A.append(i) # Append the value to a list
... print(A) # Printing the list after appending an item to it
...
[1]
[1, 2]
[1, 2, 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With