🐍 Python Beginner

What is the difference between a list and a dictionary?

Answer

A list is an ordered sequence accessed by integer indices (0-based). It stores items without explicit keys: fruits = ["apple", "banana", "cherry"]; access by index: fruits[0]. A dictionary is an unordered (insertion-ordered in Python 3.7+) mapping that stores key-value pairs: user = {"name": "Alice", "age": 25}; access by key: user["name"]. Dictionaries provide O(1) average lookup time regardless of size (like a hash map). Lists provide O(1) access by index but O(n) search by value. Use lists when the order matters and data is accessed by position; use dictionaries when data has meaningful labels/keys and fast lookup by key is needed.