Note append adds one item; extend adds each element from an iterable. Using append with a list nests it: [1].append([2,3]) gives [1, [2,3]].
add to listappend iteminsert at positionextend listpush to list
remove, pop, del, clear
Syntax
list.remove(value)list.pop(index)dellist[index]
Example
items =["a","b","c","d","e"]
items.remove("c")print(items)
last = items.pop()print(last, items)del items[0]print(items)
Output
['a', 'b', 'd', 'e']
e ['a', 'b', 'd']
['b', 'd']
Note remove() deletes the first matching value (raises ValueError if missing). pop() removes by index and returns the value. del removes by index without returning.
Note .sort() modifies the list in place and returns None. sorted() returns a new list. Use key= for custom ordering logic.
sort listorder listreverse listcustom sortsort by key
List Comprehensions
Syntax
[expression for item in iterable if condition]
Example
prices =[10,25,50,75,100]
affordable =[p for p in prices if p <=50]print(affordable)
squares =[n **2for n inrange(1,6)]print(squares)
Output
[10, 25, 50]
[1, 4, 9, 16, 25]
Note Comprehensions are more Pythonic and faster than equivalent for-loop-with-append patterns. Keep them simple; if nesting gets deep, use a regular loop.