Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I append a None value to a list in Python?

I have a list :

A = ['Yes']

I want to have

A = ['Yes',None]

How can I do this?

like image 874
salamey Avatar asked Apr 23 '14 08:04

salamey


People also ask

Does append return None?

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.

How do you add a null in Python?

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.

Why does list append return None in Python?

Because the list. append() method does not have a return value. We need to return the list after appending to it.

How do you replace None in Python?

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!


2 Answers

Just use append:

A.append(None)

>>> print A
['Yes', None]
like image 93
sshashank124 Avatar answered Oct 05 '22 20:10

sshashank124


Simple:

A.append(None)

or

A += [None]

or

A.extend([None])

or

A[len(A):] = [None]
like image 29
Sufian Latif Avatar answered Oct 05 '22 20:10

Sufian Latif