Data Structures in Python
- pencil27 Nov 2021
- clock6 min read
Data structure is a way of storing and organising data. In Python, there are four built-in data structures:
This post focuses on the syntax and fundamental capabilities of the data structures.
List
A list, as its name suggests, keeps data in a list. A list is an ordered collection, i.e. items in a list follow the order they were added into the list.
There are a few ways to instantiate a list:
Note that in Python, items in a list need not be of the same data type. You can also have a list as one of the items.
Python lists are mutable. This means that the list can be changed:
- value of items in the list can be changed
- items can be added or removed from the list
To add items to a list:
To remove items from a list:
To retrieve an item from a list:
A list can be used for multiple assignments, or sequence unpacking:
Other useful methods of a Python list:
Tuple
Tuple is a collection of items that are ordered and immutable. This means that, unlike a list, a tuple once created cannot be changed: value of items in a tuple cannot be changed, new items cannot be added, and no items can be removed from a tuple.
There are a few ways to instantiate a tuple:
To retrieve an item from a tuple:
With tuples, you can perform sequence unpacking and sequence packing.
Other useful methods of a Python tuple:
When will you use a tuple over a list?
Both list and tuple are ordered collections, but the key difference is that list is mutable while tuple is immutable.
You should use a tuple to store data if you know that it will not be changed or when you do not want them changed.
- Tuples are more memory-efficient to store
- Tuples are more time-efficient to retrieve its items.
- Tuples can be used as keys in a dictionary while lists cannot.
Set
A set is an unordered and mutable collection of objects. Since it is unordered, there is no certainty about the order the items in a set will appear.
In a set there is no duplicate item. You usually use a set when the existence of an object in a collection is more important than the order or how many times it occurs.
To create a set:
The greatest advantage of a set over a list is its cost efficiency in searching. Searching in a list will induce a worst case efficiency of $n$ where $n$ is the length of the list, while searching in a set will induce a worst case efficiency of 1 regardless of the size of the set.
Dict
A dictionary is an ordered and mutable collection of items in the form of a key-value pair. Each item in a list has a key and a value. The key is used to retrieve the value of the item. No duplicates are allowed in a dict, which means that each key can only appear once in a dict.
To create a dict:
To remove items from a dict:
As mentioned above, a tuple can be used as a key due to its immutability, while a list cannot be used. Note, however, that the tuple cannot contain a list in this case or it cannot be used as a key as well.
Similar to a set, a huge advantage of a dict is its cost efficiency in searching, which is 1 regardless of the size of the dictionary.
Conclusion
There are 4 built-in data structures in Python:
- list: ordered and mutable
- tuple: ordered and immutable
- set: unordered and mutable
- dict: ordered and mutable
From these 4 fundamental data structures, more complex data structures like a queue, stack or graph can be built.
Author: Chia Jing Heng (andreusjh99)