Default return values for out-of-bounds list item

G

gburdell1

Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item
that is out of bounds? Basically, it would be a function like this:

def item(x,index,default):
try:
return x[index]
except IndexError:
return default

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this. What if, for example, we could do something like a
[1000,44] ?
 
S

Steven D'Aprano

Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item that
is out of bounds?
No.


Basically, it would be a function like this:

def item(x,index,default):
try:
return x[index]
except IndexError:
return default

That's probably the best way to do it.

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this. What if, for example, we could do something like a [1000,44] ?

You can use slicing instead:
a=[0,1,2,3]
a[2:3] [2]
a[100:101]
[]


and then detect the empty list and use default:

def item(x, index, default):
a = x[index:index+1]
return a[0] if a else default
 
M

MRAB

Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item
that is out of bounds? Basically, it would be a function like this:

def item(x,index,default):
try:
return x[index]
except IndexError:
return default

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this.
>
There's no built-in method or notation for that.
> What if, for example, we could do something like a [1000,44] ?

That's actually using a tuple (1000, 44) as the index. Such tuples can
be used as keys in a dict and are used in numpy for indexing
multi-dimensional arrays, so it's definitely a bad idea.

If such a method were added to the 'list' class then the best option (ie
most consistent with other classes) would be get(index, default=None).
 
T

Tim Chase

MRAB said:
Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item
that is out of bounds? Basically, it would be a function like this:

def item(x,index,default):
try:
return x[index]
except IndexError:
return default

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this.
There's no built-in method or notation for that.
What if, for example, we could do something like a [1000,44] ?

That's actually using a tuple (1000, 44) as the index. Such tuples can
be used as keys in a dict and are used in numpy for indexing
multi-dimensional arrays, so it's definitely a bad idea.

If such a method were added to the 'list' class then the best option (ie
most consistent with other classes) would be get(index, default=None).

But there's nothing stopping you from creating your own subclass
of "list" that allows for defaults:

class DefaultList(list):
def __init__(self, default, *args, **kwargs):
list.__init__(self, *args, **kwargs)
self.default = default
def __getitem__(self, index):
try:
return list.__getitem__(self, index)
except IndexError:
return self.default
ml = DefaultList(42, [1,2,3,4])
for i in range(-5,5):
print i, ml

(yeah, there's likely a "proper" way of using super() to do the
above instead of "list.__<whatever>__" but the above worked for a
quick 3-minute example composed in "ed"). The output of the
above is

-5 42
-4 1
-3 2
-2 3
-1 4
0 1
1 2
2 3
3 4
4 42

One might want to add other methods for __add__/__radd__ so that
a DefaultList is returned instead of a "list", but this is
python...the sky's the limit.

-tkc
 
P

Peter Otten

Steven said:
Is there a built-in method in python that lets you specify a "default"
value that will be returned whenever you try to access a list item that
is out of bounds?
No.


Basically, it would be a function like this:

def item(x,index,default):
try:
return x[index]
except IndexError:
return default

That's probably the best way to do it.

So if a=[0,1,2,3], then item(a,0,44)=0, item(a,1,44)=1, and item(a,
1000,44)=44, item(a,-1000,44)=44

What I want to know is whether there is a built-in method or notation
for this. What if, for example, we could do something like a [1000,44] ?

You can use slicing instead:
a=[0,1,2,3]
a[2:3] [2]
a[100:101]
[]


and then detect the empty list and use default:

def item(x, index, default):
a = x[index:index+1]
return a[0] if a else default

You need to special-case -1:
.... print "x[%d] -> %r" % (i, item("abc", i, "default"))
....
x[-4] -> 'default'
x[-3] -> 'a'
x[-2] -> 'b'
x[-1] -> 'default'
x[0] -> 'a'
x[1] -> 'b'
x[2] -> 'c'
x[3] -> 'default'

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top