If you’re building data pipelines then you’re going to need to be familiar with the different types of Python data structures. It would not be befitting to not know how to use these to your advantage.
You will often find yourself either creating them or working with them when returned from library methods.
In this post, I attempt to explain what each of them are and when you might use the different types.
What are Python data structures?
Simply put, they are containers in memory for data with each having different properties. There are 4 basic data structures that are commonly used.
What types exist?
This post will focus on those four basic types:
- Lists
- Sets
- Dictionaries
- Tuples
This table summarises the differences between them:
Type/Attribute | Ordered | Duplicates | Multi-Type | Nested | Mutable |
---|---|---|---|---|---|
List | Yes | Yes | Yes | Yes | Yes |
Set | No | No | Yes | No | No |
Tuple | Yes | Yes | Yes | Yes | No |
Dictionary | Yes | No | Yes | Yes | Yes |
Lists
Lists are like arrays. Set them up using the square brackets []. They can hold multiple values and multiple data types as well as allowing nesting of values. They are ordered. Each value added is appended unless specifically determined to be at a specific position in index of the list. The following as valid.
Example:
[1, 2, 3, '4', '5', 6.0, 7.0, [8, 9, 10]]
Sets
A set is a variable which is used to store multiple items but not duplicates. Items are non-changeable, unordered and unindexed. You can add and remove items in a set but not update them. It is possible to store multiple types but nesting is not allowed. Create a set using the curly braces {}
Example:
{"potato","cucumber","carrot"}
Tuples
Tuples can store multiple items. They are indexed, ordered, allow duplicates, nesting and multi-types. They are completely unchangeable (no adding, deleting or updating). Create tuples using standard curved brackets.
Example:
("A","B","C", 1, 2, 3, ("X", "Y", "Z"))
Dictionaries
Dictionary variables in Python are like JSON objects. You create them with the curly brackets and they are used to store key value pairs. Multiple types and nesting are allowed. They are indexed, ordered but they do not allow duplicates.
Example:
{ "Title" : "The Quick Brown Fox", "Price" : 10, "Publisher" : "Some Publisher" }
If you haven’t worked with these types before, experiment with them in your Python interface. Practice reading them, looping over their values and also updating them.
Photo by Roman Serdyuk on Unsplash
I hope you found this post useful.