devnotes.

Lists: Basics and Methods

A list is Python's mutable, ordered, general-purpose sequence type — able to hold any mix of types and resize freely, roughly comparable to a vector in other languages.

fruits = ["apple", "mango", "banana"]
fruits.append("kiwi")           # add to the end
fruits.insert(1, "orange")       # insert at a specific index
fruits.remove("mango")           # remove by value (first match)
last = fruits.pop()               # remove and return the last item
fruits.sort()                      # sort in place
fruits.reverse()                    # reverse in place

mixed = [1, "two", 3.0, [4, 5]]      # lists can hold mixed types, even nested lists
print(len(fruits))                   # number of elements
print(fruits[1:3])                    # slicing works on lists too

Lists are mutable — unlike strings, methods like .append(), .sort(), and .reverse() modify the list in place and return None, which is why fruits = fruits.sort() is a common beginner mistake (it sets fruits to None). Use sorted(fruits) instead when you want a new sorted list without touching the original.

Because a list can hold values of any type, including other lists, it's flexible enough for most everyday collection needs — though for large numeric datasets, NumPy arrays (outside the standard library, but ubiquitous in practice) are far more memory- and computation-efficient. list.append() (O(1) amortized) is efficient; list.insert(0, x) (inserting at the front, O(n)) is not, and should be avoided in a hot loop in favor of collections.deque when frequent front-insertion is needed.

Python Lists: Basics and Methods | DevNotes