1.4 Advanced Data Collections
Advanced Data Collections in Python
Python provides powerful built-in data collections for storing and organizing multiple values. The four fundamental collection types are List, Tuple, Dictionary, and Set.
Choosing the correct collection is important because each one has different characteristics, such as ordering, mutability, indexing, uniqueness, and key-based access.
- Python collections and their characteristics
- Lists and list operations
- List indexing and slicing
- List methods
- Tuples and tuple operations
- Tuple unpacking
- Dictionaries and key-value pairs
- Dictionary methods and iteration
- Sets and unique values
- Set operations
- Mutability and immutability
- Nested collections
- Practical Data Science examples
1. What Are Data Collections?
A collection is an object that can contain multiple values.
Instead of creating separate variables for every value, related values can be stored together in a collection.
Without a Collection
student1 = "Alex"
student2 = "Jordan"
student3 = "Taylor"
Using a List
students = ["Alex", "Jordan", "Taylor"]
Collections make programs easier to organize, process, and scale.
2. Python Collection Types
| Collection | Ordered | Mutable | Duplicates | Access |
|---|---|---|---|---|
| List | Yes | Yes | Allowed | Index |
| Tuple | Yes | No | Allowed | Index |
| Dictionary | Insertion order preserved | Yes | Keys must be unique | Key |
| Set | No indexing | Yes | Not allowed | Membership |
List = ordered and changeable
Tuple = ordered and fixed
Dictionary = key-value mapping
Set = unique values
3. Lists in Python
A list is an ordered and mutable collection that can contain multiple values.
Lists are created using square brackets [].
numbers = [10, 20, 30, 40]
names = ["Alex", "Jordan", "Taylor"]
mixed = [10, "Python", 3.14, True]
A list can contain values of different data types.
4. List Indexing
Each element in a list has an index. Python uses zero-based indexing.
colors = ["Red", "Green", "Blue", "Yellow"]
| Value | Positive Index | Negative Index |
|---|---|---|
| Red | 0 |
-4 |
| Green | 1 |
-3 |
| Blue | 2 |
-2 |
| Yellow | 3 |
-1 |
Accessing Elements
print(colors[0])
print(colors[2])
print(colors[-1])
Output:
Red
Blue
Yellow
5. List Slicing
Slicing extracts a portion of a sequence.
Syntax
list[start:stop:step]
The stop index is excluded.
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[1:4])
Output:
[20, 30, 40]
Other Examples
print(numbers[:3]) # First three
print(numbers[3:]) # From index 3 onward
print(numbers[::2]) # Every second element
print(numbers[::-1]) # Reverse list
6. Modifying Lists
Lists are mutable, meaning their contents can be changed after creation.
Changing an Element
scores = [75, 82, 91]
scores[1] = 88
print(scores)
Output:
[75, 88, 91]
Changing Multiple Elements
scores[0:2] = [80, 85]
print(scores)
7. Important List Methods
| Method | Purpose | Example |
|---|---|---|
append() |
Adds one item at the end. | items.append(50) |
extend() |
Adds multiple items. | items.extend([60, 70]) |
insert() |
Inserts an item at a position. | items.insert(1, 25) |
remove() |
Removes the first matching value. | items.remove(25) |
pop() |
Removes and returns an item. | items.pop() |
sort() |
Sorts the list in place. | items.sort() |
reverse() |
Reverses the list in place. | items.reverse() |
clear() |
Removes all elements. | items.clear() |
index() |
Returns the index of a value. | items.index(30) |
count() |
Counts occurrences. | items.count(30) |
8. append() vs extend()
These two methods are frequently confused.
append()
numbers = [1, 2]
numbers.append([3, 4])
print(numbers)
Output:
[1, 2, [3, 4]]
extend()
numbers = [1, 2]
numbers.extend([3, 4])
print(numbers)
Output:
[1, 2, 3, 4]
append() adds one object as a single element,
while extend() adds elements from an iterable.
9. Copying Lists
Assigning a list to another variable does not create an independent copy.
original = [10, 20, 30]
copy_list = original
copy_list[0] = 100
print(original)
Output:
[100, 20, 30]
Both variables refer to the same list object.
Creating a Shallow Copy
original = [10, 20, 30]
copy_list = original.copy()
copy_list[0] = 100
print(original)
print(copy_list)
The original list remains unchanged in this example.
10. List Comprehension
A list comprehension provides a concise way to create a list from an iterable.
Traditional Approach
squares = []
for number in range(1, 6):
squares.append(number ** 2)
print(squares)
List Comprehension
squares = [number ** 2 for number in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
With a Condition
even_numbers = [
number
for number in range(1, 11)
if number % 2 == 0
]
print(even_numbers)
Use comprehensions for simple transformations and filtering. For complex logic, a normal loop is usually easier to read.
11. Tuples in Python
A tuple is an ordered, immutable collection.
Tuples are generally written using parentheses
().
coordinates = (28.6139, 77.2090)
student = ("Alex", 18, "Computer Science")
A tuple can contain different data types.
12. Single-Element Tuple
A comma is required to create a tuple containing a single item.
value = (10,)
print(type(value))
Without the comma:
value = (10)
print(type(value))
The second example creates an integer, not a tuple.
13. Accessing Tuple Elements
Tuples support indexing and slicing just like lists.
data = ("Python", "NumPy", "Pandas", "Matplotlib")
print(data[0])
print(data[-1])
print(data[1:3])
Output:
Python
Matplotlib
('NumPy', 'Pandas')
14. Tuple Immutability
Once a tuple is created, its elements cannot be reassigned.
coordinates = (10, 20)
coordinates[0] = 100
This raises a TypeError.
- To represent fixed collections of values.
- To communicate that data should not be changed.
- Tuples can be used as dictionary keys when their contents are hashable.
15. Important Tuple Methods
| Method | Purpose |
|---|---|
count() |
Counts occurrences of a value. |
index() |
Returns the index of the first matching value. |
values = (10, 20, 10, 30)
print(values.count(10))
print(values.index(30))
16. Tuple Unpacking
Tuple unpacking assigns tuple elements to multiple variables.
student = ("Alex", 18, "Physics")
name, age, subject = student
print(name)
print(age)
print(subject)
Output:
Alex
18
Physics
Practical Example
coordinates = (28.61, 77.20)
latitude, longitude = coordinates
print(latitude)
print(longitude)
17. Extended Iterable Unpacking
The * operator can collect multiple remaining values
during unpacking.
numbers = (10, 20, 30, 40, 50)
first, *middle, last = numbers
print(first)
print(middle)
print(last)
Output:
10
[20, 30, 40]
50
18. Dictionaries in Python
A dictionary stores data as key-value pairs.
Dictionaries are created using curly braces
{}.
student = {
"name": "Alex",
"age": 18,
"course": "Data Science"
}
Each key identifies a corresponding value.
19. Accessing Dictionary Values
Using Square Brackets
student = {
"name": "Alex",
"age": 18
}
print(student["name"])
Using get()
print(student.get("name"))
print(student.get("city"))
get() returns None when the requested
key does not exist, unless a default value is supplied.
print(student.get("city", "Unknown"))
20. Adding, Updating and Removing Dictionary Items
Add an Item
student["city"] = "London"
Update an Item
student["age"] = 19
Remove an Item
student.pop("city")
Remove the Last Inserted Item
student.popitem()
Clear the Dictionary
student.clear()
21. Important Dictionary Methods
| Method | Purpose |
|---|---|
keys() |
Returns dictionary keys. |
values() |
Returns dictionary values. |
items() |
Returns key-value pairs. |
get() |
Safely retrieves a value. |
update() |
Adds or updates multiple entries. |
pop() |
Removes a specified key. |
popitem() |
Removes and returns the last inserted pair. |
clear() |
Removes all items. |
22. Iterating Through a Dictionary
Iterating Over Keys
student = {
"name": "Alex",
"age": 18,
"course": "Data Science"
}
for key in student:
print(key)
Iterating Over Values
for value in student.values():
print(value)
Iterating Over Key-Value Pairs
for key, value in student.items():
print(f"{key}: {value}")
Use items() when both the key and value are
required.
23. Dictionary Keys
Dictionary keys must be hashable. Common examples include strings, integers, and tuples containing hashable values.
data = {
"name": "Alex",
101: "Student",
(10, 20): "Coordinate"
}
Mutable objects such as lists cannot be used as dictionary keys.
24. Sets in Python
A set is a mutable collection of unique elements.
Sets are useful when duplicate values need to be removed or when mathematical set operations are required.
numbers = {10, 20, 30, 20, 10}
print(numbers)
Duplicate values are automatically eliminated.
25. Creating an Empty Set
An empty set must be created using set().
items = set()
print(type(items))
Using {} creates an empty dictionary, not an empty set.
items = {}
print(type(items))
26. Important Set Methods
| Method | Purpose |
|---|---|
add() |
Adds one element. |
update() |
Adds multiple elements. |
remove() |
Removes an element; raises an error if absent. |
discard() |
Removes an element without raising an error if absent. |
pop() |
Removes and returns an arbitrary element. |
clear() |
Removes all elements. |
27. Mathematical Set Operations
Python sets support common mathematical operations such as union, intersection, difference, and symmetric difference.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Union
print(A | B)
Combines elements from both sets.
Intersection
print(A & B)
Returns elements common to both sets.
Difference
print(A - B)
Returns elements present in A but not B.
Symmetric Difference
print(A ^ B)
Returns elements present in either set but not both.
28. Set Operations Quick Reference
| Operation | Operator | Method | Meaning |
|---|---|---|---|
| Union | | |
union() |
All elements from both sets. |
| Intersection | & |
intersection() |
Common elements. |
| Difference | - |
difference() |
Elements only in the first set. |
| Symmetric Difference | ^ |
symmetric_difference() |
Elements in either set but not both. |
29. Membership Testing
The in and not in operators test whether
an item belongs to a collection.
languages = ["Python", "Java", "C++"]
print("Python" in languages)
print("Ruby" not in languages)
Membership testing is particularly useful with sets and dictionaries.
Dictionary Membership
student = {
"name": "Alex",
"age": 18
}
print("name" in student)
For a dictionary, in checks keys by default.
30. Mutable vs Immutable Collections
Mutability refers to whether an object can be changed after it has been created.
| Object | Mutable? |
|---|---|
| List | Yes |
| Tuple | No |
| Dictionary | Yes |
| Set | Yes |
| String | No |
Mutable Example
numbers = [10, 20, 30]
numbers[0] = 100
print(numbers)
Immutable Example
numbers = (10, 20, 30)
# numbers[0] = 100 # TypeError
31. Nested Collections
Collections can contain other collections. This is called a nested collection.
List of Lists
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2])
Output:
6
Dictionary Containing a List
student = {
"name": "Alex",
"subjects": ["Python", "Statistics", "AI"]
}
print(student["subjects"][0])
32. List of Dictionaries
A list of dictionaries is a common way to represent structured records before loading data into tools such as pandas.
students = [
{
"name": "Alex",
"score": 88
},
{
"name": "Jordan",
"score": 94
},
{
"name": "Taylor",
"score": 79
}
]
for student in students:
print(student["name"], student["score"])
This structure resembles a collection of database records and is a useful bridge to working with pandas DataFrames.
33. Dictionary Comprehension
Dictionary comprehensions provide a concise way to create dictionaries.
squares = {
number: number ** 2
for number in range(1, 6)
}
print(squares)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
34. Set Comprehension
Sets can also be created using comprehensions.
unique_lengths = {
len(word)
for word in ["Python", "Data", "Science", "AI"]
}
print(unique_lengths)
The resulting set contains only unique lengths.
35. sorted() vs sort()
Python provides both the sort() method and the
sorted() function.
sort()
numbers = [40, 10, 30, 20]
numbers.sort()
print(numbers)
sort() changes the original list.
sorted()
numbers = [40, 10, 30, 20]
result = sorted(numbers)
print(numbers)
print(result)
sorted() creates and returns a new sorted list.
36. Sorting with key=
The key parameter allows sorting according to a
specific property.
students = [
{"name": "Alex", "score": 82},
{"name": "Jordan", "score": 95},
{"name": "Taylor", "score": 76}
]
students.sort(key=lambda student: student["score"])
print(students)
This sorts the records according to their scores.
37. Data Science Example — Unique Categories
Sets are useful for identifying unique categories in a dataset.
departments = [
"Sales",
"IT",
"HR",
"Sales",
"Finance",
"IT",
"HR"
]
unique_departments = set(departments)
print(unique_departments)
Duplicate department names are automatically removed.
38. Data Science Example — Frequency Counting
A dictionary can be used to count the frequency of values.
subjects = [
"Python",
"AI",
"Python",
"Data Science",
"AI",
"Python"
]
frequency = {}
for subject in subjects:
frequency[subject] = frequency.get(subject, 0) + 1
print(frequency)
Output:
{
"Python": 3,
"AI": 2,
"Data Science": 1
}
Frequency counting is a fundamental data-processing operation and prepares you for later work with pandas and other Data Science libraries.
39. Automation Example — File Extensions
Dictionaries and loops can be combined to count file types.
files = [
"report.pdf",
"sales.xlsx",
"data.csv",
"summary.pdf",
"students.csv"
]
extensions = {}
for filename in files:
extension = filename.split(".")[-1]
extensions[extension] = extensions.get(extension, 0) + 1
print(extensions)
This basic pattern can later be combined with
pathlib for real file-system automation.
40. Which Collection Should You Use?
| Requirement | Recommended Collection |
|---|---|
| Ordered data that may change | List |
| Fixed ordered data | Tuple |
| Key-value relationships | Dictionary |
| Unique values | Set |
| Removing duplicates | Set |
| Records identified by keys | Dictionary |
| Sequential indexed data | List |
41. Python Interview Questions
Q1. What is the difference between a list and a tuple?
View Answer
Both are ordered and support indexing, but lists are mutable whereas tuples are immutable.
Q2. What is the difference between a list and a set?
View Answer
Lists are ordered, indexed collections that allow duplicates. Sets store unique elements and do not support positional indexing.
Q3. Why are dictionary keys required to be hashable?
View Answer
Dictionary keys are used for hash-based lookup, so they must have a stable hash value during their lifetime.
Q4. What is the difference between append() and extend()?
View Answer
append() adds one object as a single list
element, whereas extend() adds elements from
an iterable individually.
Q5. What is list slicing?
View Answer
List slicing extracts a portion of a list using the
start:stop:step notation.
Q6. What is the purpose of a set?
View Answer
Sets are useful for storing unique values, removing duplicates, membership testing, and performing mathematical set operations.
Q7. What is tuple unpacking?
View Answer
Tuple unpacking assigns the individual elements of a tuple to multiple variables in a single statement.
42. Examination Questions
Multiple Choice Questions
Q1. Which Python collection is mutable and ordered?
- Tuple
- Set
- List
- Frozen set
Answer: C — List
Q2. Which collection stores key-value pairs?
- List
- Tuple
- Set
- Dictionary
Answer: D — Dictionary
Q3. Which collection automatically removes duplicate values?
- List
- Tuple
- Set
- Dictionary
Answer: C — Set
Q4. What is the output?
values = [10, 20, 30, 40]
print(values[1:3])
[10, 20][20, 30][20, 30, 40][10, 20, 30]
Answer: B — [20, 30]
Short Answer Questions
- Define a list and state two of its characteristics.
- Differentiate between a list and a tuple.
- Explain the purpose of dictionaries with an example.
- What is a set? State two applications of sets.
- Explain list slicing with an example.
-
Differentiate between
append()andextend(). - Explain dictionary keys and values.
- What is tuple unpacking?
43. Practical Challenge
Build a Student Data Manager
Create a Python program that stores and processes student information using multiple collection types.
- Create a list containing at least five student records.
- Represent each record using a dictionary.
- Store subjects for each student in a list.
- Use a set to determine all unique subjects.
- Use a tuple to store fixed information such as coordinates or academic session data.
- Calculate or display relevant information using loops.
- Sort the students according to their scores.
44. Advanced Data Collections Quick Reference
| Collection | Syntax | Main Strength | Example |
|---|---|---|---|
| List | [] |
Ordered, changeable sequence. |
[10, 20, 30]
|
| Tuple | () |
Fixed ordered sequence. |
(10, 20, 30)
|
| Dictionary | {key: value} |
Key-value mapping. |
{"id": 101}
|
| Set | {1, 2, 3} |
Unique values. |
{10, 20, 30}
|
45. Key Takeaways
- Python provides powerful built-in collections for managing multiple values.
- Lists are ordered and mutable.
- Tuples are ordered and immutable.
- Dictionaries store key-value pairs.
- Sets store unique elements.
- Python uses zero-based indexing for lists and tuples.
-
Slicing follows the
start:stop:steppattern. -
append()andextend()behave differently. - Dictionary keys must be hashable.
- Sets are excellent for duplicate removal and membership operations.
- Nested collections can represent complex structured data.
- List, dictionary, and set comprehensions provide concise ways to create collections.
- Selecting the right collection improves code clarity, efficiency, and maintainability.
Use a List for ordered changing data, a Tuple for fixed sequences, a Dictionary for key-value relationships, and a Set when uniqueness matters.