- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Difference between list, tuple, set, and dictionary in Python
This article is created to differentiate the four built-in data structures in Python that are used to store collections of data. The four built-in data structures that are differentiated here are:
The table below distinguishes between the four built-in data structures in Python.
List | Tuple | Dictionary | Set |
---|---|---|---|
ordered | ordered | ordered | unordered |
mutable (changeable) | immutable (unchangeable) | mutable | If created with set, it is mutable. If created with frozenset, it is immutable. |
allows duplicates | allows duplicates | does not allow duplicates | does not allow duplicates |
List items enclosed within [] | Tuple items are enclosed within () | Dictionary items enclosed within {} | Set items enclosed within {} |
Lists can be created using list() | Tuples can be created using tuple() | Dictionaries can be created using dict() | Set can be created using set() |
Lists are ordered sequences of elements that can be changed. This means that you can change, add, or remove items from a list after it has been made. Lists are made by putting a series of values separated by commas inside square brackets []. For instance:
my_list = [10, 20, "codes", "cracker"]
Tuples are like lists, but their values can't be changed once they're made. Tuples are made by putting a list of values separated by commas between parentheses (). For instance:
my_tuple = (10, 20, "codes", "cracker")
Unordered collections of distinct elements make up sets. This implies that every component of a set must be distinct. A comma-separated list of values is enclosed in curly braces to form sets. For instance:
my_set = {10, 20, "codes", "cracker"}
Dictionaries are collections of key-value pairs that are not ordered. A dictionary's keys must each uniquely correspond to a value. Curly braces are used to enclose key-value pairs in dictionaries, which are key-value pairs separated by commas. For instance:
my_dict = {"key1": "value1", "key2": "value2", "key3": "value3", "key4": "value4"}
Tuples and lists are both ordered sequences of elements, but tuples are immutable and lists are mutable. Dictionary entries are key-value pairs, while sets are unordered collections of distinct elements.
« Previous Tutorial Next Tutorial »