Dictionary vs List in Python: Key Differences and Usage
list is an ordered collection of items accessed by their position (index), while a dictionary is an unordered collection of key-value pairs accessed by unique keys. Lists are best for ordered data and iteration, whereas dictionaries excel at fast lookups by keys.Quick Comparison
Here is a quick side-by-side comparison of Python list and dictionary based on key factors.
| Factor | List | Dictionary |
|---|---|---|
| Data Structure | Ordered collection of items | Unordered collection of key-value pairs |
| Access Method | By index (integer position) | By unique key (any immutable type) |
| Duplicates | Allows duplicate values | Keys must be unique |
| Use Case | Store sequences, ordered data | Store mappings, fast key lookup |
| Performance | Fast iteration, slower search by value | Fast lookup by key, slower iteration |
| Syntax Example | [1, 2, 3] | {'a': 1, 'b': 2} |
Key Differences
A list in Python is an ordered collection where items are stored in a sequence and accessed by their position using an index starting at zero. This makes lists ideal for tasks where order matters, such as storing a series of steps or elements to process in order. Lists can contain duplicate values and different data types mixed together.
On the other hand, a dictionary stores data as key-value pairs. Each key must be unique and is used to quickly access its associated value. Dictionaries are unordered (in versions before Python 3.7) but maintain insertion order in Python 3.7 and later. They are perfect when you want to associate meaningful keys (like names or IDs) with values and retrieve data efficiently without searching through the entire collection.
Performance-wise, accessing an item by index in a list is very fast, but searching for a value requires scanning the list. Dictionaries provide very fast access by key using a hash table, making them better for lookups. However, dictionaries use more memory due to storing keys and values.
Code Comparison
Here is how you store and access data using a list in Python:
fruits = ['apple', 'banana', 'cherry'] print(fruits[1]) # Access second item fruits.append('date') print(fruits)
Dictionary Equivalent
Here is how you store and access similar data using a dictionary in Python:
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'cherry': 'red'}
print(fruit_colors['banana']) # Access value by key
fruit_colors['date'] = 'brown'
print(fruit_colors)When to Use Which
Choose a list when you need to maintain order and work with a simple sequence of items, especially when duplicates are allowed or expected. Lists are great for tasks like storing user inputs, processing items in order, or when you need to iterate through elements sequentially.
Choose a dictionary when you need to associate unique keys with values for fast lookup, such as storing user profiles by ID, counting occurrences, or mapping names to data. Dictionaries are best when quick access by a meaningful key is more important than order.