0
$\begingroup$

This question already has an answer here:

i'm trying to retrieve an index to a variable and then using that variable to access the list.

The (simplified) code:

myList = {1, 5};
minElement = Position[myList, Min[myList]];
myList[[minElement]]

I get this error:

Part::pkspec1: The expression {{1}} cannot be used as a part specification.

The whole idea is to access other lists as well using the minElement index.

This seems like an easy question but i just can't find help about it.

$\endgroup$

marked as duplicate by m_goldberg, Mr.Wizard list-manipulation Aug 7 '17 at 12:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ The results of Position are to be used with Extract. E.g. Extract[myList, minElement] $\endgroup$ – Edmund Aug 7 '17 at 3:04
  • $\begingroup$ Thanks Edmund, this seems to work! $\endgroup$ – Emiliano Aug 7 '17 at 3:08
  • $\begingroup$ @Emiliano I've found that frustrating, too. As @Edmund pointed out, Extract is the way to go. But in some cases I've gone for myList[[#]] & @@@ minElement instead, which gives the same answer. $\endgroup$ – aardvark2012 Aug 7 '17 at 3:33
  • $\begingroup$ If you just need the position of the minimum, you could use Ordering[]. $\endgroup$ – J. M. will be back soon Aug 7 '17 at 4:02
  • $\begingroup$ 115226 $\endgroup$ – Kuba Aug 7 '17 at 7:02
0
$\begingroup$

It's a formatting problem. maybe use, in a non-elegant way

myList = {1, 5};
minElement = Position[myList, Min[myList]][[1]][[1]];
myList[[minElement]]

which returns

1

as expected

$\endgroup$

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