Python has four types of data collection in python: Lists, Tuples, Sets and Dictionaries. Each of these data has a useful purpose in Python - and using them properly is key to mastering Python. When to use which, and why we have four, can be confusing. In this guide, I'll go through what each of the types is, and how to use them. I have written in-depth guides on each here which go into much more detail on how to define and use each data structure.
Company Mentioned
Python has four types of data collection. When to use which, and why we have four, can be confusing. In this guide, I'll go through what each of the types is, 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 unchangeable 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 has specific uses cases:
lists are useful when we have data that 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, 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 has a useful purpose in Python - and using them properly is key to mastering Python. I have written in-depth guides on each here which go into much more detail on how to define and use each data structure. To learn more - click below: