I have a list :
A = ['Yes']
I want to have
A = ['Yes',None]
How can I do this?
append returns a reference to the original list, but it actually returns None. The solution is to call . append without assigning the result to anything.
But some you may want to assign a null value to a variable it is called as Null Value Treatment in Python. Unlike other programming languages such as PHP or Java or C, Python does not have a null value. Instead, there is the 'None' keyword that you can use to define a null value.
Because the list. append() method does not have a return value. We need to return the list after appending to it.
Use a list comprehension to replace None values in a list in Python, e.g. new_list_1 = ['' if i is None else i for i in my_list] . The list comprehension should return a different value, e.g. an empty string or 0 if the list item is None , otherwise it should return the list item. Copied!
Just use append
:
A.append(None)
>>> print A
['Yes', None]
Simple:
A.append(None)
or
A += [None]
or
A.extend([None])
or
A[len(A):] = [None]
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