0
$\begingroup$

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$
6
  • 2
    $\begingroup$ The results of Position are to be used with Extract. E.g. Extract[myList, minElement] $\endgroup$
    – Edmund
    Commented Aug 7, 2017 at 3:04
  • $\begingroup$ Thanks Edmund, this seems to work! $\endgroup$
    – Emiliano
    Commented Aug 7, 2017 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$ Commented Aug 7, 2017 at 3:33
  • $\begingroup$ If you just need the position of the minimum, you could use Ordering[]. $\endgroup$ Commented Aug 7, 2017 at 4:02
  • $\begingroup$ 115226 $\endgroup$
    – Kuba
    Commented Aug 7, 2017 at 7:02

1 Answer 1

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.