This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson
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:
- Python Data Collections
- Python Data Collections: Lists
- Python Data Collections: Tuples
- Python Data Collections: Sets
- Python Data Collections: Dictionaries
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson
Johnny Simpson | Sciencx (2022-10-01T17:16:49+00:00) Python: The Difference Between Sets, Lists, Dictionaries and Tuples. Retrieved from https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.