Course Progress25% complete
🐍 Python Basics Topic 25 / 100
⏱ 9 min read

List Operations

Adding, removing, sorting, and searching — all the ways to work with and transform lists in Python.

"A list you can only read is a decoration. The real power comes when you start adding, removing, sorting, and searching."

— ShurAI

Adding Items

python
cart = ["rice", "dal"]

# append() — add one item to the END
cart.append("milk")
print(cart)    # ['rice', 'dal', 'milk']

# insert(index, item) — add at a specific position
cart.insert(1, "bread")
print(cart)    # ['rice', 'bread', 'dal', 'milk']

# extend() — add multiple items at once
cart.extend(["eggs", "butter"])
print(cart)    # ['rice', 'bread', 'dal', 'milk', 'eggs', 'butter']

Removing Items

python
items = ["pen", "pencil", "eraser", "ruler", "pencil"]

# remove() — remove first occurrence of a value
items.remove("pencil")
print(items)     # ['pen', 'eraser', 'ruler', 'pencil'] — only first removed

# pop() — remove and RETURN the last item
last = items.pop()
print(last)      # pencil
print(items)     # ['pen', 'eraser', 'ruler']

# pop(index) — remove and return item at index
second = items.pop(1)
print(second)    # eraser

# clear() — remove everything
items.clear()
print(items)     # []

Sorting a List

python
scores = [45, 92, 17, 68, 83]
names  = ["Vikram", "Riya", "Arjun", "Sneha"]

# sort() — sort IN PLACE (changes original)
scores.sort()
print(scores)           # [17, 45, 68, 83, 92]

scores.sort(reverse=True)
print(scores)           # [92, 83, 68, 45, 17]

# sorted() — returns a NEW sorted list (original unchanged)
sorted_names = sorted(names)
print(sorted_names)     # ['Arjun', 'Riya', 'Sneha', 'Vikram']
print(names)            # ['Vikram', 'Riya', 'Arjun', 'Sneha'] — unchanged!

# reverse() — flip the order in place
names.reverse()
print(names)            # ['Sneha', 'Arjun', 'Riya', 'Vikram']
sort() vs sorted()

list.sort() changes the list directly and returns None. sorted(list) leaves the original untouched and returns a new sorted copy. Use sorted() when you need the original preserved.

Searching in a List

python
fruits = ["mango", "banana", "apple", "mango"]

# in — check if item exists (returns True/False)
print("apple" in fruits)     # True
print("guava" in fruits)     # False

# index() — find position of first occurrence
print(fruits.index("banana")) # 1
print(fruits.index("mango"))  # 0 — finds first

# count() — how many times does item appear
print(fruits.count("mango"))  # 2
print(fruits.count("guava"))  # 0

Combining and Copying Lists

python
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# + joins two lists into a new one
combined = list1 + list2
print(combined)        # [1, 2, 3, 4, 5, 6]

# * repeats a list
print([0] * 5)         # [0, 0, 0, 0, 0]

# copy() — safe copy (not just another name)
original = [10, 20, 30]
copy     = original.copy()
copy.append(40)
print(original)        # [10, 20, 30] — unchanged!
print(copy)            # [10, 20, 30, 40]

Real Example — Student Score Manager 🏫

python
scores = []

# Collect scores from user
print("Enter 5 student scores:")
for i in range(1, 6):
    s = int(input(f"  Score {i}: "))
    scores.append(s)

# Analysis
print(f"\nScores     : {scores}")
print(f"Sorted     : {sorted(scores, reverse=True)}")
print(f"Highest    : {max(scores)}")
print(f"Lowest     : {min(scores)}")
print(f"Average    : {sum(scores)/len(scores):.1f}")
print(f"Above avg  : {[s for s in scores if s > sum(scores)/len(scores)]}")
terminal
Enter 5 student scores:
  Score 1: 72
  Score 2: 88
  Score 3: 65
  Score 4: 91
  Score 5: 79

Scores     : [72, 88, 65, 91, 79]
Sorted     : [91, 88, 79, 72, 65]
Highest    : 91
Lowest     : 65
Average    : 79.0
Above avg  : [88, 91, 79]

"append, remove, sort, search — four operations that cover 90% of what you'll ever do with a list."

— ShurAI

🧠 Quiz — Question 1

What is the difference between append() and extend()?

🧠 Quiz — Question 2

What does pop() do?

🧠 Quiz — Question 3

What is the difference between .sort() and sorted()?

🧠 Quiz — Question 4

How do you check if "mango" is in a list called fruits?