Formation 90 min read Data Structures

πŸ“¦ Lists and Tuples in Python

Python & Data Science Chapter : Data Structures Sub-chapter : Lists and tuples

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
PYTHON
\nmy_list = [1, 2, 3]\nmy_list.append(4)\n
\nπŸ“Œ Tuples:\n
PYTHON
\nmy_tuple = (1, 2, 3)\n

Practical examples

πŸ’» Example: Todo list\n
PYTHON
\ntodos = []\ntodos.append("Learn Python")\nfor task in todos:\n    print(task)\n

Best 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