Comparing Collections

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


Table 17. Features of Python Collections

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.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.98.208