✦ Osnove Pythona Kolekcije

Concept 08 · List Operations & Methods

Promenite listu
jasnim operacijama.

Lists can grow, shrink, and change as your program works with a group of values.

01 Uči02 Isprobaj03 Predvidi

Metode liste menjaju listu na korisne načine.

Use <code>append()</code> to add at the end and <code>insert()</code> to add at a chosen position.

tasks = ["Learn Python", "Practice loops"]
tasks.append("Build a project")
print(tasks)

append() adds one new item after the current final item.

['Learn Python', 'Practice loops', 'Build a project']

remove(value) removes a matching value. pop(index) removes an item at an index and returns it.

↳

Važna razlika: remove() looks for a value; pop() works with a position and gives the removed item back.

Uklonite i sačuvajte stavku
items = ["book", "pen", "bag"]
items.remove("pen")
removed = items.pop(1)
print(items)
print(removed)

After removing "pen", the list shifts left, so index 1 then refers to "bag".

Proverite listu
numbers = [5, 10, 15]
print(len(numbers))
print(10 in numbers)

len() counts items and in checks whether a value is present.

Python radni prostor

Učitavanje Python okruženja…

Run each operation, then change an item or method and observe how the list changes.

list_operations.pyPython
IzlazČeka se Python
Okruženje pregledača se učitava. I dalje možeš da menjaš primer koda.

Pyodide pokreće Python u Web Worker-u pregledača. Zaustavlja se i ponovo pokreće ako se kod predugo izvršava. Izvršavanje u pregledaču nije potpuna bezbednosna granica; ne unosi tajne i ne oslanjaj se na njega za izolaciju nepouzdanog koda.

Šta će ovaj program ispisati?

Follow the list after append, then remove the item at index 1 with pop.

numbers = [5, 10, 15]

numbers.append(20)
numbers.pop(1)

print(numbers)
Izaberi izlaz
Sloby, maskota Python AI Tutora

Tvoj mentor za učenje

Ask Sloby about list operations

Describe what feels unclear about adding, removing, or changing list items.

Zdravo! Ja sam Sloby. Hajde da prođemo kroz operacije nad Python listama.

What will this program print, and why? Explain remove, pop, and the changed index.

items = ["book", "pen", "bag"]

items.append("laptop")
items.remove("pen")
removed = items.pop(1)

print(items)
print(removed)
Python AI Tutor · Uči razumevanjem, a ne samo pokretanjem koda.