Pour insérer en python un élément en première position dans une liste, il existe la méthode insert():
>>> l = ['b','c','d','e','f']>>> l.insert(0,'a')>>> l['a', 'b', 'c', 'd', 'e', 'f']
On peut aussi utiliser cette méthode pour insérer un élément à une position quelconque:
>>> l = ['a','b','d','e','f']>>> l.insert(2,'c')>>> l['a', 'b', 'c', 'd', 'e', 'f']
Autre exemple en utilisant index():
>>> l = ['a','b','d','e','f']>>> l.insert(l.index('b')+1,'c')>>> l['a', 'b', 'c', 'd', 'e', 'f']
Références
| Liens | Site |
|---|---|
| insert() | python doc |
| Insert at first position of a list in Python | stackoverflow |
| index() | programiz |
