Lists
Python - A Quick Start for existing Programers
4 min read
Published Sep 16 2025, updated Sep 30 2025
Guide Sections
Guide Comments
A list in Python is a built-in, ordered, mutable (changeable), and iterable collection of elements.
- Ordered: Items have a defined order and can be accessed by index (
list[0]). - Mutable: You can add, remove, or change items after creation.
- Heterogeneous: Can store different data types in the same list.
- Indexed: Zero-based indexing (
list[0]is the first element). []: Lists are defined by using[]square brackets.
Examples:
Python lists are indexed collections, however you can also use negative indexing:
- Zero-based indexing → the first element is at index
0, the second at1, and so on. - Negative indexing → counts from the end:
-1is the last element,-2the second-to-last, etc.
Examples:
Built-in functions used with lists
Python has a number of built in functions that can be used with lists:
len(list)→ number of elementsmin(list),max(list)→ smallest/largest elementsum(list)→ sum of elements (if numeric)sorted(list)→ returns a new sorted list (does not modify original)enumerate(list)→ get index and value pairsany(list),all(list)→ logical checks
Examples:
Lists own functions
Lists also have their own functions that can be called by using the . notation between the variable name and the function name:
Function | Description |
| Add an item |
| Add all elements from another iterable (like a list/tuple) to the end. |
| Insert item |
| Remove the first occurrence of |
| Remove and return the item at index |
| Remove all items (makes the list empty). |
| Return the first index of value |
| Return the number of occurrences of |
| Sort the list in ascending order (in-place). Optional |
| Reverse the list in place. |
| Return a shallow copy of the list. |
Examples:
Casting to a list in
You can call list(x) to create a list from any iterable (something you can loop over).
Works with:
- Strings → breaks into characters
- Tuples → converts into list of elements
- Sets → unordered elements become a list
- Dictionaries → by default, only keys are taken
- Range objects → numbers expanded into a list
- Other iterables (generators, iterators, files, custom classes that implement
__iter__)
Does not work with non-iterables (like int, float, None) → raises TypeError.
Examples:
List comparisons
In Python, lists are compared lexicographically (like words in a dictionary):
- Compare the first elements of each list.
- If they differ, that decides the result.
- If they’re equal, move to the next pair of elements.
- If all compared elements are equal but one list is shorter, the shorter one is considered less.
Rules for List Comparisons
- Uses standard comparison operators:
<,<=,>,>=,==,!=. - Works only if the elements themselves are comparable (
<,>must make sense for the element types). - If element types are not comparable (e.g.,
[1] < ["a"]), it raises aTypeError. - Think of list comparison like comparing words:
"cat" < "dog"because"c" < "d". - For lists,
[1, 2, 3] < [1, 2, 4]because3 < 4.
Examples:
List Comprehension
- A concise way to create lists.
- Equivalent to a
forloop but shorter and more readable.
Syntax:
Benefits:
- Cleaner & faster than loops.
- Can include conditions (
if). - Can nest comprehensions (though readability can suffer).
Examples:














