Learning Python – Post 6: Lists (Create, Update, Add, Remove)

Learning Python – Post 6: Lists (Create, Update, Add, Remove)

This week was all about lists. Creating them, changing them, adding to them, and removing stuff — here’s what I worked through:.

1. Creating a List

liste = [1, 2, 3, 4, 5]

Lists can hold numbers, strings, or mixed types. Square brackets [] are used.

2. Accessing Items (Indexing)

liste[0]    # first item → 1
liste[-1] # last item → 5

3. Updating Values

liste[0] = 100
print(liste) # [100, 2, 3, 4, 5]

4. Adding Items

liste.append(6)          # adds to the end
liste.insert(2, 2.5) # inserts at index 2

5. Removing Items

del liste[2]        # deletes by index
liste.remove(4) # removes first matching value

remove() only deletes the first match.
To remove all instances of a value:

while 4 in liste:
liste.remove(4)

Final Result:

liste = [4, 1, 2, 3, 4]
liste.remove(4)
print(liste) # [1, 2, 3, 4]

I find lists super flexible — especially now that I know how to modify them.

Leave a Reply

Your email address will not be published. Required fields are marked *