Types of Lists in Python (Based on Content & Use)
There is technically only one list type in Python (list
), but lists can be categorized by the types of elements they store or how they're structured. Here’s a breakdown with examples:
1. Homogeneous List
A list where all elements are of the same type.
Example: List of Integers
numbers = [1, 2, 3, 4, 5]
print(numbers)
Example: List of Strings
fruits = ["apple", "banana", "cherry"]
print(fruits)
2. Heterogeneous List
A list that contains elements of different data types.
Example:
mixed = [1, "hello", 3.14, True]
print(mixed)
3. Nested List (Multi-dimensional List)
A list that contains other lists as its elements — used to create matrices, tables, etc.
Example: 2D Matrix
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # Output: 6
4. Empty List
A list with no elements. Useful as a placeholder or to start collecting items dynamically.
Example:
empty = []
print(len(empty)) # Output: 0
5. List of Lists
A type of nested list where each item is a list — often used for structured data like rows and columns.
Example:
class_scores = [
["Alice", 85],
["Bob", 90],
["Charlie", 95]
]
print(class_scores[0][1]) # Output: 85
6. List with Boolean Values
Used often in logical filtering or flags.
Example:
flags = [True, False, True, False]
print(flags.count(True)) # Output: 2
7. List of Functions or Objects
Lists can hold functions, classes, or even custom objects.
Example: List of Functions
def greet(): return "Hello"
def bye(): return "Goodbye"
funcs = [greet, bye]
print(funcs[0]()) # Output: Hello
Bonus: List with List Comprehension
Python supports list comprehensions — a concise way to create lists.
Example: Squares of numbers
squares = [x**2 for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
Summary Table
List Type | Example | Use Case |
---|---|---|
Homogeneous | [1, 2, 3] |
When all elements are similar |
Heterogeneous | [1, "A", 3.5, True] |
When elements vary in type |
Nested / 2D List | [[1,2],[3,4]] |
Matrix or tabular data |
Empty List | [] |
Initializing before adding items |
List of Lists | [['A', 85], ['B', 90]] |
Structured records |
Boolean List | [True, False, True] |
Logical operations or filters |
Function/Object List | [func1, func2] |
Storing references to functions |
List Comprehension | [x*2 for x in range(5)] |
Dynamic list creation |
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.