-4

I want to create a program that gives you the position of the string in a list.

a = [1,3,4,5,6,7,8,9,2,"rick",56,"open"]
Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
  • 2
    The position of every string in list or the position of a specific string in a list? – Omer Dagry Nov 17 '22 at 10:05
  • Does [this](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-in-a-list?rq=1) answer your question? – treuss Nov 17 '22 at 10:08
  • 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) – Joe Nov 17 '22 at 10:17

2 Answers2

1

You should read more on operations you can do on Lists here: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists

In this case, you can use the index() function to get the index of a specific item in the list:

a=[1,3,4,5,6,7,8,9,2,"rick",56,"open"]
print(a.index(7))
print(a.index("rick"))

Output:

5
9

Remember, these indexes are 0-based, so index 5 is actually the 6th element of the list, and index 9 is the 10th element.

Adam Jaamour
  • 1,326
  • 1
  • 15
  • 31
0

If it goes to get all string elements' position:

a=[1,3,4,5,6,7,8,9,2,"rick",56,"open"]

def find_str(arr):
    res = {}
    for index, value in enumerate(arr):
        if isinstance(value, str):
            res[value] = index
    return res

or just as a shortcut:

 def find_str(arr):
    return {value:index for index, value in enumerate(arr) if isinstance(value, str)}
ruslanway
  • 285
  • 2
  • 8