Lists are one of the most used tools in Python 🧺, but they can also be a bit confusing at first. Many beginners struggle with indexing, updating, or removing items the right way 🔁. Knowing how to handle lists properly will make your code more powerful and flexible 💪.
In this blog post, we will cover how to access and update list elements, how to add or remove items, and how to loop through lists efficiently.
🚀 Let’s dive into Python lists and learn how to manage collections of data like a pro!
Video Tutorial
Indexing
fruits = ["apple", "banana", "cherry", "orange"]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[2]) # Output: cherry
print(fruits[3]) # Output: orange
print(fruits[-1]) # Output: orange (last item)
print(fruits[-2]) # Output: cherry (second last)
💡 Key Points:
- Use square brackets
[]to access items by position. - Positive numbers count from the start, negative numbers count from the end.
Slicing
fruits = ["apple", "banana", "cherry", "orange", "grape"]
print(fruits[1:4]) # ['banana', 'cherry', 'orange']
print(fruits[:3]) # ['apple', 'banana', 'cherry']
print(fruits[2:]) # ['cherry', 'orange', 'grape']
print(fruits[0:5:2]) # ['apple', 'cherry', 'grape']
💡 Key Points:
- Slicing helps extract subsets without modifying the original list.
- The start index is inclusive, and the stop index is exclusive.
- You can also use a step to skip elements.
Adding Items
append()
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
💡 Key Points:
append()adds a new item to the end of the list.
insert()
fruits.insert(1, "grape")
print(fruits) # ['apple', 'grape', 'banana', 'cherry', 'orange']
💡 Key Points:
insert(index, item)adds an item at a specific position.- Here,
"grape"is inserted at index 1 (between"apple"and"banana").
extend()
fruits.extend(["kiwi", "melon"])
print(fruits) # ['apple', 'grape', 'banana', 'cherry', 'orange', 'kiwi', 'melon']
💡 Key Points:
extend()adds multiple items to the end of the list.- It merges another list into the current one.
Changing Items
fruits = ["apple", "banana", "cherry", "orange"]
fruits[1] = "grape"
print(fruits) # ['apple', 'grape', 'cherry', 'orange']
💡 Key Points:
- You can reassign a value by directly using its index.
- Here,
"banana"at index1is replaced by"grape".
fruits[1:3] = ["kiwi", "melon"]
print(fruits) # ['apple', 'kiwi', 'melon', 'orange']
💡 Key Points:
- You can also update multiple items at once using slicing.
- Items at index
1and2("grape"and"cherry") are replaced by"kiwi"and"melon".
Removing Items
remove()
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits) # ['apple', 'cherry', 'banana']
💡 Key Points:
remove("banana")deletes the first matching item from the list.- Only the first
"banana"is removed.
pop()
fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits) # ['apple', 'cherry']
fruits.pop()
print(fruits) # ['apple']
💡 Key Points:
pop(index)removes and returns the item at that index.- Without an index, it removes the last item.
del
fruits = ["apple", "banana", "cherry"]
del fruits[0]
print(fruits) # ['banana', 'cherry']
fruits = ["apple", "banana", "cherry", "orange"]
del fruits[1:3]
print(fruits) # ['apple', 'orange']
💡 Key Points:
delcan be used to delete specific items or slices of the list.
clear()
fruits.clear()
print(fruits) # []
💡 Key Points:
clear()empties the entire list.
Loop Through a List
A simple for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
💡 Key Points:
- A simple
forloop to go through each item in the list. fruitis the loop variable that takes each item one at a time.
range(len())
for i in range(len(fruits)):
print(f"{i}: {fruits[i]}")
💡 Key Points:
- Uses
range()andlen()to loop by index. - Useful when you need both index and value.
enumerate()
for index, fruit in enumerate(fruits):
print(f"{index}: {fruit}")
💡 Key Points:
enumerate()gives both the index and value in a clean and readable way.- It’s often the most Pythonic choice when looping with positions.
Code
👉 Download












