8.1 Built in data structures
Python provides several built-in data structures to help organize and manage data efficiently. Three commonly used data structures are lists, tuples, and dictionaries. Each serves different purposes and has distinct characteristics.
8.1a Why use different data structures?
Different data structures are used to efficiently solve different problems. Here’s why we use these specific data structures:
-
Lists are great for ordered collections where you need to access, add, remove, or change items dynamically. They are versatile and widely used.
-
Tuples are useful when you need an immutable, ordered collection. They ensure that the data remains constant, which can prevent accidental modifications and can be used as dictionary keys.
-
Dictionaries provide a way to map unique keys to values, offering quick lookups, additions, and updates. They are ideal for associative arrays, databases, or any situation where you need to connect unique identifiers to specific data points.
By understanding and using these different data structures appropriately, you can write more efficient, readable, and maintainable Python code.