Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty values in a Python list

Tags:

python

list

Is it possible to assign a list with empty values in Python?
This is possible:

x = []

But I want to do something like:

x = [4,7,2, ,8,9,1]

with an empty value inside. The reason is that I have a large number of lists with 7 values and sometimes one of the values are unavailable. I need to keep say 8 as x[4],9 as x[5] etc.

My thoughts:
Maybe I should just put a number like 999 inside, or a string "empty" and tell my code to ignore 999 or "empty" or whatever.

What is the best option here?

like image 697
Kantura Avatar asked Oct 06 '14 15:10

Kantura


3 Answers

Use None for the "empty" value.

a = [1,2,3,4,5]
b = [11,22,None,44,55]
like image 66
Tapan Chandra Avatar answered Oct 12 '22 13:10

Tapan Chandra


Another option other than using None is to use some other unique object such as empty = object() as this allows None to be used as a list item and then test if the list item is empty via l[x] is empty.

like image 3
Dan D. Avatar answered Oct 12 '22 12:10

Dan D.


Use "None". It indicates that there is no value.

like image 1
Brendan8497 Avatar answered Oct 12 '22 13:10

Brendan8497