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 ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access