Welcome back to the Data Impala Python learning journey!
We explored functions, arguments, and return values in the last lesson, which taught us how to organize our logic and reuse code effectively.
Now, let’s move on to something equally powerful — advanced list techniques.
Lists are one of the most frequently used features in Python. They’re simple, flexible, and can handle just about anything — from storing names to holding complex datasets.
In this lesson, we’ll uncover how to use list comprehensions and other advanced list tricks to make your code clean, efficient, and expressive.
Table of Contents
Recap: What Is a List?
Before we jump into the advanced stuff, let’s refresh what a list is.
A list in Python is a collection of items, enclosed in square brackets [].
It can hold numbers, text, or even other lists.
fruits = ["apple", "banana", "cherry"]
numbers = [10, 20, 30, 40]
mixed = ["data", 99, 3.14, True]
You can think of lists as containers that hold multiple pieces of data in one place.
The Power of List Comprehension
In Python, list comprehension is a shortcut that helps you create lists in a single line — without using long loops.
Let’s start with an example.
Traditional Way:
squares = []
for x in range(1, 6):
squares.append(x * x)
print(squares)
This creates a list of square numbers [1, 4, 9, 16, 25].
Using List Comprehension:
squares = [x * x for x in range(1, 6)]
print(squares)
Both versions give the same result, but the second one is shorter and more readable.
Breaking Down the Syntax
The structure of list comprehension is:
| [expression for item in iterable if condition]_ |
Let’s understand each part:
expression → What you want to do with each item (e.g., x * x)
for item in iterable → Loops through the sequence (like range(1,6))
if condition (optional) → Filters items that meet a condition
Example 1: Filtering Data
Suppose you have a list of numbers, and you only want the even ones.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)
Output: [2, 4, 6, 8]
Here’s what happened:
- Python looped through each number.
- It checked if the number was even (n % 2 == 0).
- If true, it added it to the new list.
This is cleaner than writing a loop with multiple lines and append() calls.
Example 2: Changing Data in a List
You can also use list comprehension to transform items.
prices = [100, 200, 300, 400]
discounted = [price * 0.9 for price in prices]
print(discounted)
Output: [90.0, 180.0, 270.0, 360.0]
This reduced all prices by 10%.
You can apply any operation to modify data — rounding, converting types, cleaning text, and more.
Example 3: Adding Conditions
You can add an if-else right inside a list comprehension.
numbers = [3, 7, 10, 12, 15]
result = ["Even" if n % 2 == 0 else "Odd" for n in numbers]
print(result)
Output: [‘Odd’, ‘Odd’, ‘Even’, ‘Even’, ‘Odd’]
It checks each number and labels it accordingly.
Nested List Comprehension
Sometimes you have lists inside lists — a concept called nested lists.
You can flatten them or process them using nested comprehensions.
Example: Flattening a List of Lists
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flat = [num for row in matrix for num in row]
print(flat)
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
- The first loop for row in matrix picks each sublist.
- The second loop for num in row picks individual numbers.
- All numbers are collected into one single list.
Common List Tricks
Beyond comprehension, Python gives you many list methods to work efficiently.
Let’s explore some useful ones with explanations.
1. sort() and sorted()
They both arrange list items in order, but work slightly differently.
numbers = [5, 2, 9, 1]
numbers.sort() # Sorts in place
print(numbers) # [1, 2, 5, 9]
new_list = sorted(numbers, reverse=True)
print(new_list) # [9, 5, 2, 1]
- .sort() changes the original list.
- sorted() creates a new one.
2. reverse()
Flips the order of items.
names = ["Anna", "Bob", "Charlie"]
names.reverse()
print(names)
Output: [‘Charlie’, ‘Bob’, ‘Anna’]
3. count()
Counts how many times an element appears.
letters = ["a", "b", "a", "c", "a"]
print(letters.count("a")) # Output: 3
4. extend()
Adds multiple items from another list (or iterable).
fruits = ["apple", "banana"]
more_fruits = ["cherry", "mango"]
fruits.extend(more_fruits)
print(fruits)
Output: [‘apple’, ‘banana’, ‘cherry’, ‘mango’]
5. copy()
Creates a separate copy of the list.
original = [1, 2, 3]
duplicate = original.copy()
duplicate.append(4)
print(original) # [1, 2, 3]
print(duplicate) # [1, 2, 3, 4]
This is useful when you want to modify a list but keep the original safe.
6. pop() and remove()
Both delete elements, but in different ways:
- .pop(index) removes an item by position and returns it.
- .remove(value) deletes the first matching item.
numbers = [10, 20, 30, 40]
numbers.pop(2) # Removes item at index 2
numbers.remove(40) # Removes item with value 40
print(numbers)
Output: [10, 20]
7. join() and split() with Lists
You can turn lists into strings (and vice versa) using join() and split().
words = ["Data", "Impala", "Python"]
sentence = " ".join(words)
print(sentence) # "Data Impala Python"
split_words = sentence.split(" ")
print(split_words) # ['Data', 'Impala', 'Python']
Practice Challenge
Task:
You have a list of numbers.
Create a new list that contains:
- Squares of only the even numbers
- Ignore odd ones
List:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Try writing your own solution first before checking below.
Solution:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_squares = [n * n for n in numbers if n % 2 == 0]
print(even_squares)
Output: [4, 16, 36, 64, 100]
Explanation:
- The comprehension loops through each n in numbers.
- It checks if n is even (n % 2 == 0).
- If true, it squares it (n * n) and adds it to the new list.
Wrapping Up
In this lesson, you learned how to:
- Use list comprehension to build lists faster
- Add conditions inside comprehension
- Work with nested lists
- Apply common list methods for real tasks
Mastering these techniques will make your Python code faster, shorter, and easier to understand.
In the next lesson, we’ll have discuss about Object-Oriented Programming (OOP) in Python. Stay tuned.


