Creating functions in a loop with names depends on the iterable

My goal : get the digit in the position i from a number x with a function called ni.

e.g : ( i = 3 , and x = 12345 ) => n3(12345) == 4.

My task surrounds a lot of number manipulation and I can have numbers up to 10**24.

I want to create 24 functions : n1 – n2 .. n24 at one go in a for loop like this :

for i in range(1,25):
    def n`i`(x):
        return int(str(x)[i])

In general, how can I automate the creation of functions from a list of data ?

I am still new to coding and your advices is much appreciated !


Just what you asked for.

for i in range(1, 10):
    def fn(x):
        return int(str(x)[i])
    globals()[f"n{i}"] = fn  # update global variables dictionary

#use them like

But I recommend defining a function that takes both arguments and using it

def ni(i,x):
   return int(str(x)[i])

ni(3, 123456)