Skip to content · ⁨Passer au contenu⁩

Find the position · ⁨Trouver la position⁩

English

Find the position

Search a list and report where something is, counting from 0.

Loop over the positions with range(len(items)), so you have the index i as well as the item items[i]. return leaves the function straight away, which is exactly what you want at the first match. Return -1 only after the loop has checked every item.

Français

Trouver la position

Recherchez dans une liste et indiquez où se trouve un élément, en comptant à partir de 0.

Parcourez les positions avec range(len(items)), afin d'avoir l'index i ainsi que l'élément items[i]. return quitte immédiatement la fonction, ce qui est exactement ce que vous voulez au premier match. Retournez -1 uniquement après que la boucle ait vérifié tous les éléments.

Log in or create account · ⁨Se connecter ou créer un compte⁩

IGCSE, A-Level & AP