Manipulating Data Structures
Practice sorting, slicing, and iterating.
Manipulating Data Structures is a free Python For Kids lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Manipulating Data Structures
Welcome to the Data Structures lesson! Today, we'll learn how to manipulate data structures like lists and tuples by sorting, slicing, and iterating through them.

2
What is Data Manipulation?
Data manipulation involves changing, organizing, or analyzing the data stored in data structures. It helps you manage and utilize data effectively in your programs.
- Sorting: Arranging data in a specific order.
- Slicing: Extracting a portion of the data.
- Iterating: Going through each item in the data structure.
3
Sorting Lists
Sorting arranges the items in a list in a particular order, such as ascending or descending. Python provides built-in methods to sort lists easily.
Example:
# Creates a list of numbers
numbers = [4, 2, 9, 1, 5]
# Sorts the list in ascending order
numbers.sort()
# Prints the sorted list
print(numbers) # Output: [1, 2, 4, 5, 9]4
Slicing Lists and Tuples
Slicing allows you to access a subset of items from a list or tuple. You can specify the start and end indices to extract a portion of the data.
Example:
# Creates a list of fruits
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
# Slices the list from index 1 to 3 (excluding 4)
subset = fruits[1:4]
# Prints the sliced list
print(subset) # Output: ['banana', 'cherry', 'date']
5
Iterating Through Data Structures
Iterating means going through each item in a data structure one by one. Loops, such as for and while loops, are used to iterate through lists and tuples.
Example:
# Creates a list of colors
colors = ["red", "green", "blue"]
# Iterates through the list and prints each color
for color in colors:
print(color)
6
List Comprehensions
List comprehensions provide a concise way to create lists. They can make your code shorter and more readable.
Example:
# Creates a list of squares from 1 to 5 using list comprehension
squares = [x**2 for x in range(1, 6)]
# Prints the list of squares
print(squares) # Output: [1, 4, 9, 16, 25]
7
Sorting with Keys
You can sort lists based on specific criteria using the key parameter in the sort() method.
Example:
# Creates a list of tuples with names and ages
people = [("Alice", 30), ("Bob", 25), ("Charlie", 35)]
# Sorts the list by age using a key
people.sort(key=lambda person: person[1])
# Prints the sorted list
print(people) # Output: [('Bob', 25), ('Alice', 30), ('Charlie', 35)]
8
Example Program: Student Grades
Let's create a program that sorts students by their grades and slices the top performers.
Code:
# Creates a list of student grades as tuples
student_grades = [
("Alice", 88),
("Bob", 92),
("Charlie", 78),
("Diana", 85),
("Ethan", 95)
]
# Sorts the list by grade in descending order
student_grades.sort(key=lambda student: student[1], reverse=True)
# Slices the top 3 students
top_students = student_grades[:3]
# Prints the top students
print("Top Students:")
for student in top_students:
print(student[0] + " scored " + str(student[1]))
# Output:
# Top Students:
# Ethan scored 95
# Bob scored 92
# Alice scored 88
9
# Creates a list of numbers
numbers = [10, 5, 8, 3, 7]
# Sorts the list in ascending order
numbers.sort()
# Slices the first three numbers
top_three = numbers[:3]
# Prints the sliced list
print(top_three)
10
Great Job!
You've learned how to manipulate data structures in Python by sorting, slicing, and iterating through lists and tuples. These skills help you organize and manage data effectively in your programs. Keep practicing by experimenting with different data structures and their manipulation techniques!

Frequently asked questions
Is the “Manipulating Data Structures” lesson free?
Yes — the full text of “Manipulating Data Structures” is free to read here on the web, and the Python For Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Manipulating Data Structures”?
Practice sorting, slicing, and iterating. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Manipulating Data Structures” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python For Kids lesson?
Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lists and Tuples
- Dictionaries
- Sets
- Manipulating Data Structures