Python – When to use list vs. tuple vs. dictionary vs. set (Theory)


Data Structures

Python supports the following data structures: lists, dictionaries, tuples, sets.

When to use a dictionary:
– When you need a logical association between a key:value pair.
– When you need fast lookup for your data, based on a custom key.
– When your data is being constantly modified. Remember, dictionaries are mutable.

When to use the other types:
– Use lists if you have a collection of data that does not need random access. Try to choose lists when you need a simple, iterable collection that is modified frequently.
– Use a set if you need uniqueness for the elements.
– Use tuples when your data cannot change.

Many times, a tuple is used in combination with a dictionary, for example, a tuple might represent a key, because it’s immutable.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s