Python has four types of data collections. When to use which, and why we have four, can be confusing. In this guide I'll go through what each of the types are, and how to use them. The four types of data collection in python are:
- lists: which are ordered, changeable and can contain duplicate values, and indexed by number
- tuples: which are ordered, unchangeable, and can contain duplicate values.
- sets: which are unordered, have unchangable values once set, but may have items added or deleted, and cannot contain duplicate values.
- dictionaries: which are unordered (depending on your python version), changeable, have indexes, and cannot contain duplicate values.
To put this into simpler terms, here is a table of their key properties:
† dictionaries are ordered only after Python 3.7
†† sets may have new values added, or values removed, but we cannot change values already added
You might be wondering why there are so many, but each of these have specific uses cases:
- lists are useful when we have data which may contain duplicates. They are like typical arrays in other languages such as Javascript.
- tuples, are faster than lists, but cannot be changed. This is useful when we have a set number of values to iterate through, like the column names of a table, which may contain duplicates.
-
sets, which again, are faster than lists, but whose original contents cannot be changed. We can still add and remove items, though, making them more flexible than tuples in that regard. They are a great way to test if an item is a member of a specific set of other items - i.e. if we wanted to check the word
apple
was in a set. - dictionaries, which are like lists, but with keys. These are like objects in other languages such as Javascript, and are useful for giving context to our data through key value pairs.
Learn More about Python Data Structures
Each of these types of data have a useful purpose in Python - and using them properly is key to mastering Python. I have written indepth guides on each here which go into much more detail on how to define and use each of these data structures. To learn more - click below:
Top comments (0)