.append(), .insert().remove(), .pop()in, .index(), .count().sort() vs sorted(), len()This is the opposite of strings. With a string you had to catch a new value. With a list, the methods change the same list directly.
# String — must reassign:
name = name.upper()
# List — changes itself, NO reassign:
marks = [70, 85]
marks.append(90) # marks is now [70, 85, 90]
# NOT: marks = marks.append(90) ← returns None!
marks = marks.append(90) sets marks to None and loses your list. Just call marks.append(90) on its own line.
.append() & .insert()queue = ["Aisha", "Rohit"]
queue.append("Sneha") # ['Aisha', 'Rohit', 'Sneha'] — adds at END
queue.insert(0, "Vikram") # ['Vikram', 'Aisha', 'Rohit', 'Sneha']
# insert(position, item) — squeezes in
.append(). It's the workhorse: start with an empty list [] and append as you go — exactly what you did in the initials loop yesterday.
.remove() & .pop()names = ["Aisha", "Rohit", "Sneha"]
names.remove("Rohit") # remove by VALUE → ['Aisha','Sneha']
last = names.pop() # remove by POSITION (default: last)
# last == 'Sneha', names == ['Aisha']
first = names.pop(0) # pop a specific index, returns it
.remove("x") = "delete this value". .pop(i) = "delete the item at this position and hand it back to me".
roll = ["Aisha", "Rohit", "Sneha", "Rohit"]
"Sneha" in roll # True — the membership test
"Karan" in roll # False
roll.index("Rohit") # 1 — position of FIRST match
roll.count("Rohit") # 2 — how many times it appears
len(roll) # 4 — how many items total
if "Karan" in roll: protects you — calling .remove() or .index() on a missing value raises a ValueError.
.sort() vs sorted() — know the difference# .sort() — changes the list itself
marks = [85, 70, 92]
marks.sort() # marks is now [70, 85, 92]
marks.sort(reverse=True) # [92, 85, 70]
# sorted() — returns a NEW sorted list
marks = [85, 70, 92]
ranked = sorted(marks) # [70, 85, 92]
# marks is UNCHANGED: [85, 70, 92]
list.sort() rearranges in place and returns None. sorted(list) leaves the original alone and gives you a fresh, sorted copy. Strings and numbers both sort naturally.
Everything from Module 1 still works: loop over a list, and combine these new verbs inside the loop.
roll = ["aisha", "ROHIT", "sneha"]
# Clean every name (D16) and collect into a new list
clean = []
for name in roll:
clean.append(name.title()) # build up the result
clean.sort() # alphabetical
print(clean, "·", len(clean), "students")
# → ['Aisha', 'Rohit', 'Sneha'] · 3 students
marks = marks.append(x) — .append returns None, so you lose the list..remove("x") when "x" isn't there → ValueError. Check with in first.sorted(marks) to change marks — it doesn't; catch the return..remove(value) with .pop(index) — one takes a value, the other a position.Keep a list of student names. A loop menu lets the user add, remove, search, and list all (sorted, with a count).
Refuse duplicates, handle "remove someone not on the list" gracefully, and ignore stray spaces/case on input.
students/<your-username>/D18_rollcall.py. Full brief in lab/module_2_practice/D18_rollcall/problem.md. PR before 12:00.
.append(), .remove(), .pop().
in, .index(), .count() and order with sort/sorted.
text + lists together to build a deduplicator on Day 19.