8

Say a list list={{1,2},{3,4}}, list[[1]] and list[[2]] works. But list[[3]] gives an error of

Part::partw: Part 3 of {{1,2},{3,4}} does not exist.

This is alright, since there's no part 3 indeed. But what if I wish to handle that error. So in this case, I want to return empty list {} if the part-does-not-exist error happens.

I try to refer to https://reference.wolfram.com/language/ref/Catch.html, but it seems to be involved with Throw. Therefore I try to think of a simpler scenario as above.

liang
  • 589
  • 2
  • 12

4 Answers4

6

But what if I wish to handle that error. So in this case, I want to return empty list {} if the part-does-not-exist error happens

One possibility might be

ClearAll[process];

process[lis_List, (n_Integer)?Positive] := Module[{},
       Quiet@Check[lis[[n]], {}, Part::partw]
]

Or just

process[lis_List, (n_Integer)?Positive] := Quiet@Check[lis[[n]], {}, Part::partw]

And now

list = {{1, 2}, {3, 4}};
process[list, 2]
 (*{3, 4}*)

and

process[list, 3]
(* {} *)

Quiet can be removed if you want to hear the beep and see the error message on console.

Nasser
  • 143,286
  • 11
  • 154
  • 359
5

As with the essence of @Nasser's answer, I go with a simplified version for now.

Quiet[Check[{{1, 2}, {3, 4}}[[3]], {}], Part::partw]

This returns empty list if partw message happens.

liang
  • 589
  • 2
  • 12
4

Without using Quiet or Check you can use

SafePart[expr_, n_Integer?Positive, default_ : $Failed] := 
  If[Length[expr] >= n, Part[expr, n], default];
SafePart[expr_, parts : {__Integer?Positive}, default_ : $Failed] := 
 Fold[SafePart[#1, #2, default] &, expr, parts]

then you have

SafePart[{{{a, b}, {c}}}, {1, 2, 1}]
(* c *)

SafePart[{{{a, b}, {c}}}, 3, {}]
(* {} *)
Jason B.
  • 68,381
  • 3
  • 139
  • 286
4

Have you considered Query? Default behavior:

list = {{1, 2}, {3, 4}}

Query[3][list]
Missing["PartAbsent", 3]

Somewhat customized:

fn = Replace[Missing["PartAbsent", _] -> {}];

Query[3, fn][list]
{}

Or with care:

Unprotect[Missing];
Missing["PartAbsent", _] = {};
Protect[Missing];

Query[3][list]
{}

Caveat, Query can be slow:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371