pop Metodu
pop metodu, bir listeden belirtilen indeksteki
öğeyi siler ve döndürür. Metodun parametresinde
öğenin indeksini belirtiriz. Eğer belirtilmezse
son öğe döndürülür.
Sözdizimi
liste.pop([indeks])
Örnek
Son öğeyi silelim:
lst = ['a', 'b', 'c', 'd', 'e']
lst.pop()
print(lst)
Kodun çalıştırılmasının sonucu:
['a', 'b', 'c', 'd']
Örnek
Metot, silinen öğeyi döndürür:
lst = ['a', 'b', 'c', 'd', 'e']
print(lst.pop())
Kodun çalıştırılmasının sonucu:
'e'
Örnek
İndekse göre bir öğe bulalım ve pop
metoduyla silelim:
lst = ['a', 'b', 'c', 'd', 'e']
lst.pop(2)
print(lst)
Kodun çalıştırılmasının sonucu:
['a', 'b', 'd', 'e']
Örnek
Şimdi de listede olmayan bir indeksteki öğeyi silmeyi deneyelim:
lst = ['a', 'b', 'c']
lst.pop(3)
Kodun çalıştırılmasının sonucu:
IndexError: pop index out of range