Книга: Practical Programming, Fourth Edition
Назад: Using the in Operator on Tuples, Sets, and Dictionaries
Дальше: Creating New Type Annotations

Comparing Collections

You’ve now seen strings, lists, sets, tuples, and dictionaries. They all have their uses. Here is a table comparing them:

Collection

Mutable?

Ordered?

Use When…

str

No

Yes

You want to keep track of text.

list

Yes

Yes

You want to keep track of an ordered sequence that you want to update.

tuple

No

Yes

You want to build an ordered sequence that you know won’t change or that you want to use as a key in a dictionary or as a value in a set.

set

Yes

No

You want to keep track of values, but order doesn’t matter, and you don’t want to keep duplicates. The values must be immutable.

dictionary

Yes

No

You want to keep a mapping of keys to values. The keys must be immutable.

Назад: Using the in Operator on Tuples, Sets, and Dictionaries
Дальше: Creating New Type Annotations