Lists in Python
From the Python cheat sheet ยท Data Structures ยท verified Jul 2026
Lists
Ordered, mutable sequences with powerful methods.
python
nums = [1, 2, 3, 4, 5]
nums.append(6) # Add to end
nums.insert(0, 0) # Insert at index
nums.pop() # Remove & return last
nums.remove(3) # Remove first occurrence
nums.sort() # Sort in place
len(nums) # Length
nums[1:3] # Slice [2, 3]๐ก .sort() modifies in place and returns None โ use sorted() for a new list
โก Use first, *rest = list to unpack the head and tail
๐ .remove() deletes by value; del and .pop() delete by index
๐ข Slicing never raises IndexError โ out-of-range slices return empty lists
listsdata-structures