4

How can I convert the this rule to a list:

input = {1 -> {71.52, 55.33}};

to:

output = {71.52, 55.33}
lio
  • 2,396
  • 13
  • 26

3 Answers3

9

For versions before 10.0, use Replace

Replace[1, input]
(* {71.52, 55.33} *)

I prefer to avoid ReplaceAll when Replace is appropriate. ReplaceAll works at all levels in an expression and is thus less predictable (or it makes a bigger mess if there's a bug in my code). Consider if you can potentially have multiple non-atomic keys in the rule list, some of which are sub-parts of others, e.g. {1 -> ..., a[1] -> ...}.

For 10.0 or later versions, use Lookup

Lookup[input, 1]
(* {71.52, 55.33} *)

Lookup is better for this use because it tells you if the key is missing from input.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
3

In general, you can convert list of rules into list of values, in the one of following four ways:

Last @@@ input
input[[All, 2]]
input /. Rule -> (#2 &)
Values@input
Dragutin
  • 920
  • 5
  • 14
2

it could be convenient to use Association for this, depending on what you are actually doing..

  Association[input][1]

{71.52, 55.33}

george2079
  • 38,913
  • 1
  • 43
  • 110