Python: The Difference Between Sets, Lists, Dictionaries and Tuples

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 or…

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:

Comparison of data collections in Python

† 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:


Print Share Comment Cite Upload Translate
APA
Johnny Simpson | Sciencx (2024-03-28T23:12:28+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/.
MLA
" » Python: The Difference Between Sets, Lists, Dictionaries and Tuples." Johnny Simpson | Sciencx - Saturday October 1, 2022, https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/
HARVARD
Johnny Simpson | Sciencx Saturday October 1, 2022 » Python: The Difference Between Sets, Lists, Dictionaries and Tuples., viewed 2024-03-28T23:12:28+00:00,<https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/>
VANCOUVER
Johnny Simpson | Sciencx - » Python: The Difference Between Sets, Lists, Dictionaries and Tuples. [Internet]. [Accessed 2024-03-28T23:12:28+00:00]. Available from: https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/
CHICAGO
" » Python: The Difference Between Sets, Lists, Dictionaries and Tuples." Johnny Simpson | Sciencx - Accessed 2024-03-28T23:12:28+00:00. https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/
IEEE
" » Python: The Difference Between Sets, Lists, Dictionaries and Tuples." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/. [Accessed: 2024-03-28T23:12:28+00:00]
rf:citation
» Python: The Difference Between Sets, Lists, Dictionaries and Tuples | Johnny Simpson | Sciencx | https://www.scien.cx/2022/10/01/python-the-difference-between-sets-lists-dictionaries-and-tuples/ | 2024-03-28T23:12:28+00:00
https://github.com/addpipe/simple-recorderjs-demo