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

More Python tasks

Back to the full Python cheat sheet