-1
X = [1,4,5,10,23,2,5,7,19]

I want to know the digit Nth of the list...

Example:

def func(n):
    print(nth digit of X)

def func(7):
print(7th digit of X)
Output = 3

Don't need to be a function...just the method to reach to solution of the problem: function returns nth digit of a list

zoramind
  • 25
  • 1
  • 5
  • 1
    https://stackoverflow.com/questions/509211/understanding-slice-notation –  Sep 28 '20 at 17:58
  • Actually i used sliced but it gives me the n index of the list...what i want is the digit – zoramind Sep 28 '20 at 17:59
  • 1
    Accessing elements of a list is covered in any decent python tutorial. A tutorial (many available online) or a tutor can help you better than Stack Overflow – Pranav Hosangadi Sep 28 '20 at 18:00
  • Where did the 3 come from if `X = [1,4,5,10,23,2,5,7,19]` and you want the 7th "digit"? –  Sep 28 '20 at 18:01
  • @JustinEzequiel There are 9 numbers in the list and 12 digits. – ekhumoro Sep 28 '20 at 18:12

2 Answers2

1

You can convert the list to a str, and then the indexing would work

x_str = ''.join([str(i) for i in x])  # x_str = "145102325719" 
# x_str[idx] would return the digit
lange
  • 590
  • 5
  • 9
0
x = [1,4,5,10,23,2,5,7,19]

def func(x, n):
    return ''.join(map(str, x))[n-1]

print(func(x, 7))

Prints:

3
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    While code-only answers might answer the question, you could significantly improve the quality of your answer by providing context for your code, a reason for why this code works, and some references to documentation for further reading. From [answer]: _"Brevity is acceptable, but fuller explanations are better."_ – Pranav Hosangadi Sep 28 '20 at 18:01
  • 1
    Exactly what i need it! Ty! My Recaman sequence digit founder is complete! – zoramind Sep 28 '20 at 18:04