English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La méthode index() recherche l'élément dans la liste et renvoie sa valeur d'index.
En résumé, la méthode index() dansList inFind the given element and return its position.
If the same element appears multiple times, this method returns the index of the first occurrence of the element.
Note: Note: In Python, indexing starts from 0, not1.
The syntax of the index() method is:
list.index(element)
The method has one parameter:
element -The element to be searched for.
The method returns the index of the element in the list.
If the element is not found, it will raise a ValueError exception indicating that the element is not in the list.
# Vowel list vowels = ['a', 'e', 'i', 'o', 'i', 'u'] # Index of 'e' index = vowels.index('e') print('Index value of e:', index) # Index of the first 'i' index = vowels.index('i') print('Index value of i:', index)
Output result
Index value of e: 1 Index value of i: 2
# Vowel list vowels = ['a', 'e', 'i', 'o', 'u'] # 'p' does not exist in the list index = vowels.index('p') print('Index value of p:', index)
Output result
ValueError: 'p' is not in list
# Random list random = ['a', ('a', 'b'), [3, 4] # Index of ('a', 'b') index = random.index(('a', 'b')) print("Index of ('a', 'b'):", index) # [3, 4] index index = random.index([3, 4] print("[3, 4] Index:
Output result
Index of ('a', 'b'): 1 [3, 4] Index: 2