Week 5 Tutorial: Review of Data Structures – Lists, Dictionaries, and Methods
Task 1: Introduction to Lists
1. What is a List?
o A list in Python is an ordered collection of items that can be of different types
(e.g., integers, strings, objects).
o Lists are mutable, meaning you can change their contents after creation.
2. Creating a List:
o You can create a list by placing items inside square brackets []
fruits = ["apple", "banana", "cherry"]
3. Accessing List Items:
o Access items using their index, with the first item at index 0
print(fruits[0]) # Output: apple
4. Modifying Lists:
o Lists are mutable, so you can update elements:
fruits[1] = "orange" # Change banana to orange
5. Common List Methods:
o append(item): Adds an item to the end of the list
fruits.append("kiwi") # Adds kiwi to the list
o remove(item): Removes the first occurrence of the item.
fruits.remove("apple")
6. List Comprehensions:
o A concise way to create or modify lists:
squared_numbers = [x**2 for x in range(5)] # Output: [0, 1, 4, 9, 16]
Task 2: Working with Dictionaries
1. What is a Dictionary?
o A dictionary in Python is a collection of key-value pairs. Each key must be
unique, but values can be duplicated.
o Unlike lists, dictionaries are unordered and accessed via keys, not indices.
2. Creating a Dictionary:
o Use curly braces {} to create a dictionary.
student = {"name": "Segun", "age": 21, "grade": "A"}
3. Accessing Dictionary Values:
o You can access values using keys
print(student["name"]) # Output: Segun
4. Modifying Dictionary Values:
o You can add new key-value pairs or update existing ones:
student["age"] = 22 # Update age to 22
student["major"] = "Computer Science" # Add a new key-value pair
5. Common Dictionary Methods:
o keys(): Returns all the keys in the dictionary
print(student.keys()) # Output: dict_keys(['name', 'age', 'grade', 'major'])
o values(): Returns all the values
print(student.values()) # Output: dict_values(['Segun', 22, 'A', 'Computer Science'])
6. Looping through a Dictionary:
o You can loop through both keys and values:
for key, value in student.items():
print(f"{key}: {value}")
Task 3: Methods and Their Importance in Data Structures
1. What are Methods?
o Methods are functions that belong to an object (e.g., list or dictionary). They
allow objects to perform actions.
o Lists and dictionaries in Python come with built-in methods that help you
manipulate data efficiently.
2. Using List Methods:
o sort(): Sorts the list in place.
fruits.sort() # Sorts the list alphabetically
o extend(list): Adds all elements of another list.
fruits.extend(["grape", "mango"])
o pop(): Removes and returns the last element.
last_fruit = fruits.pop() # Removes and returns the last fruit
Task 4: Student Activity – Practicing with Lists and Dictionaries
Activity 1 – List Operations:
o Create a list called shopping_list with at least five items.
o Perform the following operations:
Add two new items to the list.
Remove the second item from the list.
Sort the list in alphabetical order.
Print the final list.
Activity 2 – Dictionary Operations:
Create a dictionary called book with the following keys: title, author, and pages.
Perform the following operations:
o Add a new key-value pair for publisher.
o Update the number of pages.
o Print out the dictionary’s keys and values.