What's the difference between sets, lists, tuples, and dictionaries in python?

Part of the "coder 101" series. A description of data structures in python.

Just a quick refresher to clarify differences:

  • Sets, lists, tuples, and dictionaries are all data structures that offer various ways to organize and store data. The difference is in the details.
  • Lists are an ordered and mutable collection of elements(meaning that their values can be modified after creation).
    • Elements in a list can be any data type (mixed is permitted).
    • Duplicates are allowed
    • Lists are represented using square brackets []
  • Tuples are an ordered and immutable collection of elements (meaning that their values cannot be modified after creation).
    • Elements can be of any type (mixed is permitted).
    • Duplicates are allowed.
    • Tuples are represented using parentheses ()
  • Sets are unordered collection of unique elements
    • Elements are not stored in a specific order
    • Duplicates are automatically removed
    • Sets are represented using curly braces {}
  • Dictionaries are an unordered collection of key-value pairs.
    • Keys must be unique and are immutable (meaning that their values cannot be modified after creation).
    • Values can be any data type.
    • Python accesses elements using keys, not indexes/position.
    • Dictionaries are represented using curly braces {} with colons : to separate keys and values.