Formation
90 min read
Data Structures
π¦ Lists and Tuples in Python
Learning objectives
π― Objectives:\n
1Master lists\n2. Understand tuples\n3. Choose between list and tuple\n4. Use unpacking
Introduction
π Lists and tuples are fundamental data structures in Python.
Theoretical content
Lists:\n\nπ Tuples:\n
PYTHON
\nmy_list = [1, 2, 3]\nmy_list.append(4)\nPYTHON
\nmy_tuple = (1, 2, 3)\nPractical examples
π» Example: Todo list\n
PYTHON
\ntodos = []\ntodos.append("Learn Python")\nfor task in todos:\n print(task)\nBest practices
1Lists for mutable collections\nβ
2. Tuples for constant data\nβ
3. Use unpacking to swap variables
Common pitfalls
Modifying a list while iterating\n
Iterate over a copy
Summary
Lists: mutable, ordered\nβ
Tuples: immutable, faster\nβ
Unpacking: a, b = b, a
Additional resources
π docs.python.org/3/tutorial/datastructures.html