What is a Python list?
A list is an ordered, mutable collection written with square brackets, such as `[1, 2, 3]`.
Study 6 Python Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a Python list?
A list is an ordered, mutable collection written with square brackets, such as `[1, 2, 3]`.
How do `append()` and `extend()` differ?
`append()` adds one item to the end of a list, while `extend()` adds each item from an iterable.
What is a list comprehension?
A list comprehension creates a new list from an iterable, optionally filtering items, in a compact expression such as `[n * n for n in range(1, 6)]`.
What is a Python tuple?
A tuple is an ordered, immutable collection. Its items cannot be replaced or deleted after creation.
How do you write a one-item tuple?
The comma creates a tuple, so a one-item tuple must be written `(42,)`; `(42)` is just the integer expression `42`.
What does zero-based indexing mean?
Python uses zero-based indexing: the first item has index `0`. A negative index counts backward from the end.
What is Python slice syntax?
A slice uses `sequence[start:stop:step]`; `start` is included, `stop` is excluded, and omitted parts use the sequence boundaries as appropriate.
What does a list slice copy?
For lists, `numbers[:]` creates a shallow copy: it copies the outer list but keeps references to the same nested objects.
What does `second = first` do for a list?
Assignment creates another reference to the same object; it does not automatically copy a collection. Mutating through either reference affects that shared object.
When should you use `enumerate()`?
Use `enumerate()` when a loop needs both each element and its index, as in `for index, color in enumerate(colors):`.
What is a Python dictionary?
A dictionary maps unique, hashable keys to values. It is mutable and written with `key: value` pairs inside braces.
Why use `dict.get()`?
`get()` returns a key's value or a default when the key is absent, avoiding the `KeyError` that direct bracket lookup can raise.