Kantura
Kantura

Reputation: 6433

Empty values in a 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?

Upvotes: 9

Views: 39575

Answers (3)

Dan D.
Dan D.

Reputation: 74645

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.

Upvotes: 3

Brendan8497
Brendan8497

Reputation: 29

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

Upvotes: 1

Tapan Nallan
Tapan Nallan

Reputation: 1812

Use None for the "empty" value.

a = [1,2,3,4,5]
b = [11,22,None,44,55]

Upvotes: 27

Related Questions