Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

closed as off-topic by Öskå, RunnyKine, kale, Sjoerd C. de Vries, Michael E2 Jul 24 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.

2  
Part –  Sektor Jul 24 at 18:32

2 Answers 2

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*)
share|improve this answer
    
What happens if the OP has another List, called list2 (list2 = Range[30]^2)? –  eldo Jul 24 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 at 4:22

A slightly different way:

list = Range @ 49;

MapIndexed[(f[First @ #2] = #1)&, list]
share|improve this answer

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