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
pythonliste.pop([indeks])
Örnek
Son öğeyi silelim:
pythonlst = ['a', 'b', 'c', 'd', 'e']
lst.pop()
print(lst)
Kodun çalıştırılmasının sonucu:
python['a', 'b', 'c', 'd']
Örnek
Metot, silinen öğeyi döndürür:
pythonlst = ['a', 'b', 'c', 'd', 'e']
print(lst.pop())
Kodun çalıştırılmasının sonucu:
python'e'
Örnek
İndekse göre bir öğe bulalım ve pop
metoduyla silelim:
pythonlst = ['a', 'b', 'c', 'd', 'e']
lst.pop(2)
print(lst)
Kodun çalıştırılmasının sonucu:
python['a', 'b', 'd', 'e']
Örnek
Şimdi de listede olmayan bir indeksteki öğeyi silmeyi deneyelim:
pythonlst = ['a', 'b', 'c']
lst.pop(3)
Kodun çalıştırılmasının sonucu:
errorIndexError: pop index out of range