4.7 KiB
4.7 KiB
Sure, here's a detailed look at common data structures in Python and the types of data they are typically used for:
1. Lists
-
Common Uses:
- Ordered collections of items where the order matters.
- Storing items that can be changed, added, or removed.
- Examples: To-do lists, sequences of numbers, names of students, etc.
-
Typical Data:
- Numbers:
[1, 2, 3, 4, 5] - Strings:
['apple', 'banana', 'cherry'] - Mixed types:
[1, 'apple', 3.14, True]
- Numbers:
2. Dictionaries
-
Common Uses:
- Mapping relationships between keys and values.
- Fast lookups of values based on unique keys.
- Examples: Phone books (name to number), configuration settings, JSON-like data structures.
-
Typical Data:
- Strings to strings:
{'name': 'Alice', 'city': 'New York'} - Strings to numbers:
{'apples': 10, 'bananas': 5} - Mixed keys and values:
{1: 'one', 'two': 2, (3, 4): 'tuple_key'}
- Strings to strings:
3. Tuples
-
Common Uses:
- Fixed collections of items where the order matters and should not change.
- Returning multiple values from a function.
- Storing related but different pieces of data.
- Examples: Coordinates (x, y, z), RGB color values, database records.
-
Typical Data:
- Numbers:
(1, 2, 3) - Strings:
('red', 'green', 'blue') - Mixed types:
(1, 'apple', 3.14)
- Numbers:
4. Sets
-
Common Uses:
- Collections of unique items.
- Membership tests, eliminating duplicates.
- Mathematical set operations like union, intersection, and difference.
- Examples: Unique user IDs, unique items in a collection, tags or keywords.
-
Typical Data:
- Numbers:
{1, 2, 3, 4, 5} - Strings:
{'apple', 'banana', 'cherry'} - Mixed types (less common due to unhashable types):
{1, 'apple', (3, 4)}
- Numbers:
5. Strings
-
Common Uses:
- Storing and manipulating text.
- Examples: Sentences, file paths, URLs, JSON data.
-
Typical Data:
- Sentences:
"Hello, world!" - Paths:
"/usr/bin/python" - JSON data:
'{"name": "Alice", "age": 25}'
- Sentences:
6. Arrays (from the array module)
-
Common Uses:
- Storing large amounts of numeric data efficiently.
- Arrays provide more efficient storage and faster access for numeric data.
- Examples: Numerical computations, scientific data.
-
Typical Data:
- Numbers:
array.array('i', [1, 2, 3, 4, 5])
- Numbers:
7. Namedtuples (from the collections module)
-
Common Uses:
- Creating simple classes to group related data.
- Similar to tuples, but with named fields for better readability.
- Examples: Representing rows from a database, simple data structures.
-
Typical Data:
- Custom data structures:
from collections import namedtuple Point = namedtuple('Point', ['x', 'y']) point = Point(1, 2)
- Custom data structures:
8. Deques (from the collections module)
-
Common Uses:
- Double-ended queues for fast appends and pops from both ends.
- Examples: Implementing queues, stacks, and other data structures.
-
Typical Data:
- Items in a queue or stack:
from collections import deque my_deque = deque([1, 2, 3, 4, 5])
- Items in a queue or stack:
9. Defaultdict (from the collections module)
-
Common Uses:
- Dictionaries with a default value for non-existent keys.
- Examples: Counting occurrences, grouping data.
-
Typical Data:
- Dictionaries with default values:
from collections import defaultdict my_defaultdict = defaultdict(int) my_defaultdict['a'] += 1
- Dictionaries with default values:
Examples of Usage
List Example
# List of student names
students = ['Alice', 'Bob', 'Charlie']
Dictionary Example
# Dictionary of student grades
grades = {'Alice': 'A', 'Bob': 'B', 'Charlie': 'C'}
Tuple Example
# Coordinates of a point
point = (10, 20)
Set Example
# Set of unique fruits
unique_fruits = {'apple', 'banana', 'cherry'}
String Example
# A sentence
sentence = "The quick brown fox jumps over the lazy dog."
Array Example
import array
# Array of integers
int_array = array.array('i', [1, 2, 3, 4, 5])
Namedtuple Example
from collections import namedtuple
# Named tuple for representing a point
Point = namedtuple('Point', ['x', 'y'])
point = Point(1, 2)
Deque Example
from collections import deque
# Deque for queue operations
my_deque = deque(['a', 'b', 'c'])
my_deque.append('d')
my_deque.appendleft('z')
Defaultdict Example
from collections import defaultdict
# Defaultdict for counting
count = defaultdict(int)
count['apple'] += 1
Understanding these common data structures and their typical use cases will help you choose the right one for your needs in different programming scenarios.