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 set or used as a dict key.

Not hashable

Collection is mutable or cannot produce a hash value, therefore cannot be a member of a set or used as a dict key.

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                               | |                                        | |   +----------+----------+----------+   | |   |          |          |          |   | |   | first    | second   | third    |   | |   | item     | item     | item     |   | |   |          |          |          |   | |   +----------+----------+----------+   | |   |    0 cCFF|    1 cCFF|    2 cCFF|   | |   +----------+----------+----------+   | |                                        | +----------------------------------------+    /----\   |cCFF| index number   \----/

Sequence types#

Type

Description

list

mutable collection of arbitrary objects

tuple

immutable collection of arbitrary objects

set

unordered collection with no duplicate elements.

range

collection containing a arithmetic progressions of numbers

str

sequence of unicode characters

bytes

immutable collection of integers between 0 and 256

bytearray

mutable collection of integers between 0 and 256

memoryview

provide access to internal memory of bytes or bytesarray object

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

set

mutable collection of unique immutable objects

frozenset

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

+----------+----------+----------+ |          |          |          | | 1        | 2        | 3        | |          |          |          | +----------+----------+----------+ | "a"  cCFF| "b"  cCFF| "c"  cCFF| +----------+----------+----------+    /----\   |cCFF| key   \----/

Mapping types#

Type

Description

dict

mutable collection of arbitrary objects indexed by nearly arbitrary values

Reference#

Collection capabilities table#

Type

Base

Syntax

Subscriptable

Mutable

Ordered

Unique

list

Sequence

[item,...]

Yes

Yes

Yes

No

tuple

Sequence

(item,...)

Yes

No

Yes

No

str

Sequence

"text"

Yes

No

Yes

No

bytes

Sequence

b"text"

Yes

No

Yes

No

set

Set

{item,...}

No

Yes

No

Yes

dict

Mapping

{key: value,...}

Yes

Yes

Yes

No

range

Sequence

Yes

No

Yes

Yes

bytearray

Sequence

Yes

Yes

Yes

No

memoryview

Sequence

Yes

Yes

Yes

No

frozenset

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 and tuple.

See also#