# Defining a function from a list [closed]

I have a list of 49 numbers, and I would like to define a function f such that f(n) is simply the nth number in the list. How can I do this?

-

## closed as off-topic by Öskå, RunnyKine, kale, Sjoerd C. de Vries, Michael E2Jul 24 '14 at 19:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Öskå, RunnyKine, kale, Sjoerd C. de Vries, Michael E2
If this question can be reworded to fit the rules in the help center, please edit the question.

Part –  Sektor Jul 24 '14 at 18:32

list = Range[20]^2

(*{1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, \
289, 324, 361, 400}*)

f[n_] := list[[n]]

f[10]
(*100*)

-
What happens if the OP has another List, called list2 (list2 = Range[30]^2)? –  eldo Jul 24 '14 at 19:05
@eldo,he has to define which list he wants to get the nth element. if he want to get it from list2, then he can add list=list2. –  Algohi Jul 25 '14 at 4:22

A slightly different way:

list = Range @ 49;

MapIndexed[(f[First @ #2] = #1)&, list]

-