What is the difference between a stack and a queue in Python?
Stacks are LIFO and queues are FIFO. Learn how to implement both in Python with list, deque and the thread-safe queue module.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Stacks are LIFO and queues are FIFO. Learn how to implement both in Python with list, deque and the thread-safe queue module.
Python has no built-in static array; list is a dynamic array. Compare list, array, NumPy and ctypes for fixed-size and typed storage.
copy.deepcopy builds a fully independent clone of nested objects. See examples, how it differs from a shallow copy, handling of cycles, custom classes and performance tips.
copy.copy makes a shallow copy: a new outer object that shares its inner objects with the original. See examples, pitfalls and when to use copy.deepcopy instead.
Use copy.deepcopy to duplicate a dictionary and all nested objects, and learn when a shallow copy such as dict.copy is enough.
Copy a Python dictionary with copy(), dict(d), {**d} or d | {}, and see why nested lists and dicts are still shared between the original and the copy.
Shallow copies share nested objects, deep copies duplicate them. See copy.copy and copy.deepcopy in action, plus immutables, custom classes and common pitfalls.
Loop through a Python dictionary with keys, values() and items(), use enumerate and comprehensions, and avoid changing size during iteration.
Update Python dictionary values with square brackets, update(), the |= operator or a comprehension, and learn why setdefault() and nested updates behave differently.
Delete items from a Python dictionary with del, pop(), popitem(), clear() or comprehensions, and learn how to avoid KeyError and iteration errors.
Learn every way to add items to a Python dictionary: assignment, update(), merge operators, setdefault() and comprehensions, with clear examples.
Learn how to read values from a Python dictionary with brackets, get(), in checks and loops, including how to handle missing keys and nested data.
A Python dictionary key is the unique, hashable identifier used to look up a value. See which types work as keys, which do not, and how to use them safely.
Create Python dictionaries with literals, dict(), comprehensions and defaultdict, then read, update and merge them safely.
ctypes.c_voidp is an alias of c_void_p, the type for C void pointers. See how to pass handles, buffers and callbacks between Python and C safely.
ctypes.c_ssize_t is the signed integer type that matches C ssize_t. See how to declare it for return values, structures and error codes.
ctypes.c_buffer creates a writable byte buffer for C calls and is the same as create_string_buffer. See sizing, value versus raw and a working example.
ctypes.byref passes an argument to a C function by reference without building a full pointer. See output-parameter examples and how it compares with pointer.
ctypes.addressof returns the memory address of a ctypes object as an integer. See how it works with structures and arrays and how it differs from byref and pointer.
create_unicode_buffer gives C functions writable wide-character memory. Learn size units, wchar_t differences and a Windows API example.