Data Structure in Python

As we have seen in the previous lessons, Python supports the following data structures: listsdictionariestuplessets.

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 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started