0

How can i write a python function that prints out the requested index position in a list?

def index_position(my_list, index)
index = my_list[index]
for i in index:
    return index

Here I expect the argument to print out the index position of an element of a list list when the function is called.

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • 1
    Does this answer your question? [Finding the index of an item in a list](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list) – dahiya_boy Mar 15 '23 at 17:07
  • The code indentation is incorrect. Furthermore, you seem to have a confusion about return statement and for loop. – RomainL. Mar 15 '23 at 17:15

1 Answers1

0

I do not really understand what you mean, but I'll try to explain.

How can i write a python function that prints out the requested index position in a list?

This is very easy, you are actually really close to the solution:

def index_position(my_list, index):
     a = my_list[index]
     print(a)

This will surelly work by printing the element at the given index (unless its out of bounds for the list)

As for this: Here I expect the argument to print out the index position of an element of a list list when the function is called.

I understand that you want something like a getIndexOf method, that returns the index of an element that you are passing, this could be done like this:

def index_position(my_list, index):
    counter = 0;
    for i in my_list:
        if(i == index):
            print(counter)
            return
        counter += 1

This will print the the element at the position on the list.