Python Lists Tutorial - append, extend, remove, sort Methods
What is a List?
A list is an ordered, mutable collection that can hold items of any data type. Lists are one of the most used data structures in Python.
Creating a List
empty = []
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "mango"]
mixed = [1, "hello", 3.14, True, None]
matrix = [[1, 2, 3], [4, 5, 6]]
chars = list("Python") # ['P', 'y', 't', 'h', 'o', 'n']
print(len(fruits)) # 3
print(type(fruits)) # <class 'list'>Accessing Elements (Indexing & Slicing)
Lists use zero-based indexing. Negative indexes count from the end.
fruits = ["apple", "banana", "orange", "grapes", "mango"]
print(fruits[0]) # apple
print(fruits[2]) # orange
print(fruits[-1]) # mango (last item)
print(fruits[-2]) # grapes
# Slicing: list[start:stop:step]
print(fruits[1:3]) # ['banana', 'orange']
print(fruits[:3]) # ['apple', 'banana', 'orange']
print(fruits[2:]) # ['orange', 'grapes', 'mango']
print(fruits[::2]) # ['apple', 'orange', 'mango']
print(fruits[::-1]) # reversed list
# Nested list
matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[1][2]) # 6Updating a List
fruits = ["apple", "banana", "orange"]
fruits[1] = "mango" # change single item
fruits.append("pear") # add to end
fruits.insert(1, "cherry") # insert at index 1
fruits.extend(["fig", "plum"]) # add multiple items
# Concatenate
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b # [1, 2, 3, 4, 5, 6]
print([0] * 5) # [0, 0, 0, 0, 0]Deleting Elements
fruits = ["apple", "banana", "orange", "banana", "mango"]
fruits.remove("banana") # removes first occurrence
last = fruits.pop() # removes and returns last item
item = fruits.pop(1) # removes and returns item at index 1
nums = [10, 20, 30, 40, 50]
del nums[2] # delete by index
del nums[1:3] # delete a slice
nums.clear() # remove all itemsLooping Through a List
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
# With index
for i, fruit in enumerate(fruits):
print(f"{i}: {fruit}")
# List comprehension
upper = [f.upper() for f in fruits]
print(upper) # ['APPLE', 'BANANA', 'MANGO']
# Filter with comprehension
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
evens = [n for n in numbers if n % 2 == 0]
print(evens) # [2, 4, 6, 8]List Methods
| Method | Description |
|---|---|
append(x) | Add item to end |
insert(i, x) | Insert at index i |
extend(iterable) | Add all items from iterable |
remove(x) | Remove first occurrence of x |
pop(i) | Remove & return item at index (default: last) |
clear() | Remove all items |
index(x) | Return index of first occurrence |
count(x) | Count occurrences of x |
sort() | Sort in place |
reverse() | Reverse in place |
copy() | Return shallow copy |
nums = [3, 1, 4, 1, 5, 9, 2, 6]
print(nums.count(1)) # 2
print(nums.index(5)) # 4
nums.sort()
print(nums) # [1, 1, 2, 3, 4, 5, 6, 9]
nums.reverse()
print(nums) # [9, 6, 5, 4, 3, 2, 1, 1]
# sorted() returns new list
original = [3, 1, 4, 1, 5]
sorted_list = sorted(original)
print(original) # [3, 1, 4, 1, 5] unchanged
print(sorted_list) # [1, 1, 3, 4, 5]
# Sort by key
words = ["banana", "apple", "cherry", "fig"]
words.sort(key=len)
print(words) # ['fig', 'apple', 'banana', 'cherry']
# Copy vs reference
a = [1, 2, 3]
b = a.copy() # independent copy
b.append(4)
print(a) # [1, 2, 3] unchangedLevel Up Your Python Skills
Master Python with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Key Takeaways
- Python lists are ordered, mutable, and allow duplicate values.
- Use append() to add one item, extend() to add multiple, insert() for a specific position.
- List slicing syntax: list[start:stop:step] - all parameters are optional.
- List comprehensions are faster and more Pythonic than traditional for loops.
- Use copy() or list[:] to create a shallow copy - avoid aliasing bugs.
- sorted() returns a new list; list.sort() modifies in place.
Common Mistakes to Avoid
WRONG
list2 = list1
RIGHT
list2 = list1.copy()
Assignment creates a reference, not a copy. Both variables point to the same list.
WRONG
for i in range(len(mylist)): print(mylist[i])
RIGHT
for item in mylist: print(item)
Iterate directly over the list - it is cleaner and more Pythonic.
WRONG
mylist.append([1,2,3])
RIGHT
mylist.extend([1,2,3])
append() adds the entire list as one element. extend() adds each element individually.
Frequently Asked Questions
Related Python Topics