Say I have Part of a variable x.

Part[x, 1]

Then, I'll get an error message, as the variable x is not recognised as a list with multiple parts.

If I want to suppress this error, because I know for sure x will have multiple parts, I can use the Quiet function.

Quiet[Part[x, 1], {Part::partd}]

But now imagine that the list variable x itself is an element of another list y such that x=y[[1]].

Quiet[Part[Part[y, 1], 1], {Part::partd}]

I expect this to return y[[1][[1]], however, what I get is y.

Any ideas why this happens? Obviously the examples are simplified working examples, but it should work. How can I get my expected behaviour?

  • 6
    A robust way would be to use Indexed instead of Part. See also here. – Henrik Schumacher Sep 14 at 13:36
  • 2
    You get y because Part[y, 1] doesn't evaluate if y is atomic. So the outer Part then extracts the first argument of Part[y,1], which is y. This is because Part works on arbitrary expression, not just Lists – Sjoerd Smit Sep 14 at 13:45
  • @SjoerdSmit Ah, that makes sense. Though adding an Evaluate around the inner Part doesn't seem to work. The Indexed solution is much nicer anyway, as it also doesn't require the Quiet. – LBogaardt Sep 14 at 13:47
  • 1
    Evaluate will not do anything here since there's no hold attribute to override (see also mathematica.stackexchange.com/a/180500/43522) . Mathematica tries to evaluate Part[y, 1], but since there's nothing it can evaluate to, it stays the way it is. That's how Mathematica handles most ill-defined expressions: it's part of the symbolic nature of the language. Mathematica uses a term-rewriting style of evaluation and when there's no applicable rules available, things just stay the way they are. – Sjoerd Smit Sep 14 at 13:51
up vote 2 down vote accepted

Based on Henrik Schumacher's comment (feel free to answer yourself, then you'll get the credits).

A robust way would be to use Indexed instead of Part. See also here.

Example code:

Indexed[x, 1] 
" returns x_1 "
Indexed[Indexed[y, 1], 1]
" returns y_1_1 "
Indexed[Indexed[{{a, b}, {d, c}}, 1], 1]
" returns a "
myFunction[y_, i_, j_]:=Indexed[Indexed[y, i], j]
myFunction[{{a, b}, {d, c}}, 2, 1] 
" returns d "

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.