What Is the Difference Between a Dictionary and a List in Python?
Lists are accessed by position, dictionaries by key. Compare lookup speed, ordering, memory and use cases with runnable Python examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Lists are accessed by position, dictionaries by key. Compare lookup speed, ordering, memory and use cases with runnable Python examples.
Learn how Python dictionaries store key-value pairs, and how to create, read, update, loop over, merge and transform them with working examples.
zip() combines iterables position by position into an iterator of tuples. See parallel loops, uneven lengths, strict=True, dictionaries and unzipping.
enumerate() yields index and item pairs from any iterable, replacing manual counters and range(len()) loops with shorter, clearer code.
append adds one element to a Python list, extend adds each item of an iterable. See examples, pitfalls with strings, and when to use += instead.
Use the in operator for a simple membership test, any() for conditions and a set for repeated lookups. Includes complexity notes and equality pitfalls.
Python range() creates a lazy, immutable sequence of integers. Learn the three argument forms, looping patterns, and mistakes such as the exclusive stop value.
Python's try and except keywords catch runtime errors so your program keeps running. Learn else, finally, raising exceptions, except* and the common mistakes.
A practical tour of Python's built-in functions, grouped by purpose, with examples for conversion, iteration, I/O and introspection.
Python error handling uses try, except, else and finally to manage exceptions. Learn custom errors, context managers, exception groups and logging.
Read keyboard input with input(), print and format results, and read or write files in Python with working examples.
__init__ sets up a new Python object right after creation. See defaults, validation, inheritance with super() and when dataclasses replace it.
self is the first parameter of Python instance methods and refers to the current object. See how it works with attributes, inheritance, cls and static methods.
Python inheritance lets a subclass reuse and extend a parent class. See super(), overriding, multiple inheritance, the MRO and composition.
Classes and objects in Python give you encapsulation, reuse, polymorphism and modular code. See modern examples with dataclasses, protocols and ABCs.
The return statement ends a function and sends a value back to the caller. Learn early returns, multiple values, the None default and return versus print.
Define Python functions with def, then work through parameters, defaults, *args and **kwargs, return values, type hints, scope and common pitfalls.
Lists are mutable and tuples are immutable. Learn how that affects dictionary keys, performance, unpacking and which one to choose, with examples.
Assign a value to a name to declare a Python variable. Covers naming rules, type hints, multiple assignment, global and nonlocal scope and constants.
Operators, the math and statistics modules, exact Decimal arithmetic and NumPy for fast array maths in current Python.