How to check if an item exist in a nested list
Matt McCredie
mccredie at gmail.com
Thu Jul 19 17:56:32 EDT 2007
> Is there any way to check if an item in specific location in a multiple
> dimension nested exist? For example something like:
>
> if M_list[line][row][d] exist:
> do_something_0
> else:
> do_something_1
>
Certainly:
<code>
try:
M_list[line][row][d]
except IndexError:
do_something_1
else:
do_something_0
</code>
Assuming that you want to check because something in `do_someting_0' fails
if the item isn't defined, it is very likely that this can be shortened to:
<code>
try:
do_something_0
except IndexError:
do_something_1
</code>
The try/except should be wrapped as tightly as possible around the specific
code that actually throws the exception.
Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070719/3b72756b/attachment.html>
More information about the Python-list
mailing list