Containers
Contents
Containers#
Table of Contents
Introduction#
Python provides several A container or collection types–these are data structures, values that can hold other values. Individual values in a collection are referred to as elements or items.
Capabilities#
The behavior and features of any particular collection will depend on its type. Here are some of the key aspects.
State |
Mutable Collections that can be modified after they are created. |
Immutable Collections that cannot be modified once they are created. |
Position |
Ordered Elements retain their position in the collection. |
Unordered Elements are not stored in any particular order. |
Composition |
Homogeneous All elements in the collection are of the same type. |
Heterogeneous Elements in the collection may be of any type. |
Diversity |
Unique Only unique elements are added to the collection. |
Repeatable Duplicate elements are allowed in collection. |
Access |
Subscriptable Elements can be accessed via bracket notation. |
Not subscriptable Bracket notation is not supported. |
Value |
Hashable Collection is immutable and can produce a hash value, therefore it can be
a member of a |
Not hashable Collection is mutable or cannot produce a hash value, therefore cannot be a
member of a |
Sequences#
An ordered collection accessed via index numbers.
- State
Mutable
- Position
Ordered
- Composition
varies
- Diversity
Repeatable
- Access
Subscriptable
- Value
Hashable
Sequences are organized in terms of the position of each element in the collection.
Each element is assigned a successive index number, starting at 0
.
You can imagine each element grabbing a ticket from one of those “take a
number” dispensers.
The below diagram shows a visualization of a hypothetical sequence with three items.
Sequence types#
Type |
Description |
---|---|
|
mutable collection of arbitrary objects |
|
immutable collection of arbitrary objects |
|
unordered collection with no duplicate elements. |
|
collection containing a arithmetic progressions of numbers |
|
sequence of unicode characters |
|
immutable collection of integers between |
|
mutable collection of integers between |
|
provide access to internal memory of |
See also#
Sets#
An unordered collection with no duplicate elements.
- State
Mutable
- Position
Unordered
- Composition
Heterogeneous
- Diversity
Unique
- Access
Not subscriptable
- Value
Not hashable
Set types#
Type |
Description |
---|---|
|
mutable collection of unique immutable objects |
|
immutable collection of unique immutable objects |
Mappings#
A collection of key: value pairs.
- State
Mutable
- Position
Ordered
- Composition
Heterogeneous
- Diversity
Repeatable
- Access
Subscriptable
- Value
Not hashable
Mapping types#
Type |
Description |
---|---|
|
mutable collection of arbitrary objects indexed by nearly arbitrary values |
Reference#
Collection capabilities table#
Type |
Base |
Syntax |
Subscriptable |
Mutable |
Ordered |
Unique |
---|---|---|---|---|---|---|
|
Sequence |
|
Yes |
Yes |
Yes |
No |
|
Sequence |
|
Yes |
No |
Yes |
No |
|
Sequence |
|
Yes |
No |
Yes |
No |
|
Sequence |
|
Yes |
No |
Yes |
No |
|
Set |
|
No |
Yes |
No |
Yes |
|
Mapping |
|
Yes |
Yes |
Yes |
No |
|
Sequence |
Yes |
No |
Yes |
Yes |
|
|
Sequence |
Yes |
Yes |
Yes |
No |
|
|
Sequence |
Yes |
Yes |
Yes |
No |
|
|
Set |
No |
No |
No |
Yes |
Glossary#
Data Types#
- bracket notation#
- subscript#
- subscription#
Accessing an element from a collection object using
[
]
after the object which enclose a selector expression. The expression may be an index number, key, or slice depending on its type.
The syntax is:COLLECTION[SELECTOR]
.- data structure#
A particular way to store, organize, and access multiple values. A data type that can store other values.
- container#
- collection#
A value that can hold other values, for example
list
objects.- element#
- item#
An individual value in a collection.
- index#
- index number#
An integer representing an elements position in a sequence.
- slice#
Selecting a range of elements from a collection. In Python, this is done either using subscription or the
slice
type.- sequence#
A category of data types which are charactarized as ordered collection accessed via index numbers, such as
list
andtuple
.