List Comprehensions: Magic Loops
Hey there! Today, we’re going to learn about something super cool in Python called “List Comprehensions.” Imagine you’re a magician and you can create lists in a magical and easy way. That’s what list comprehensions are like!
What Are Lists?
First, let’s talk about lists. A list is like a special box where you can store many things. Imagine you have a box where you can put your toys, books, and candies. In Python, a list is a special box where you can store numbers, words, or even other lists.
Here’s how you make a list in Python:
my_list = [1, 2, 3, 4, 5]
This list has the numbers 1, 2, 3, 4, and 5.
What Are Loops?
Now, let’s talk about loops. A loop is like a magic spell that repeats an action over and over. For example, if you want to say “Hello!” five times, you can use a loop:
for i in range(5):
print("Hello!")
This will print “Hello!” five times.
List Comprehensions: The Magic Loops
List comprehensions are like magic loops that can create lists in a super easy way. They let you write a loop inside a list, which makes your code shorter and easier to understand.
Here’s the magic formula for list comprehensions:
new_list = [expression for item in list]
- new_list is the new list you’re creating.
- expression is what you want to put in the new list.
- item is each element from the original list.
- list is the original list.
Let’s see some examples!
Example 1: Creating a List of Squares
Imagine you have a list of numbers, and you want to create a new list with the squares of those numbers. Normally, you would do it like this:
numbers = [1, 2, 3, 4, 5]
squares = []
for num in numbers:
squares.append(num * num)
print(squares)
But with the magic of list comprehensions, you can do it in one line!
numbers = [1, 2, 3, 4, 5]
squares = [num * num for num in numbers]
print(squares)
Wow! That’s much easier, right?
Example 2: Creating a List of Even Numbers
Let’s say you have a list of numbers, and you want to create a new list with only the even numbers (numbers that can be divided by 2 with no remainder). Normally, you would do it like this:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = []
for num in numbers:
if num % 2 == 0:
even_numbers.append(num)
print(even_numbers)
With list comprehensions, it’s much simpler!
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in numbers if num % 2 == 0]
print(even_numbers)
Example 3: Creating a List of Letters
Let’s say you want to create a list with each letter in a word. Normally, you would do it like this:
word = "magic"
letters = []
for letter in word:
letters.append(letter)
print(letters)
With list comprehensions, you can do it in one line!
word = "magic"
letters = [letter for letter in word]
print(letters)
Practice Questions
Now it’s your turn to practice some magic!
Question 1: Create a List of Cubes
Create a list of the cubes (number * number * number) of numbers from 1 to 5.
Question 2: Create a List of Odd Numbers
Create a list of odd numbers (numbers that can’t be divided by 2 with no remainder) from 1 to 10.
Question 3: Create a List of Uppercase Letters
Create a list with each letter in the word “hello” in uppercase.
Solutions
Solution 1: Create a List of Cubes
numbers = [1, 2, 3, 4, 5]
cubes = [num * num * num for num in numbers]
print(cubes)
Output:
[1, 8, 27, 64, 125]
Solution 2: Create a List of Odd Numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
odd_numbers = [num for num in numbers if num % 2 != 0]
print(odd_numbers)
Output:
[1, 3, 5, 7, 9]
Solution 3: Create a List of Uppercase Letters
word = "hello"
uppercase_letters = [letter.upper() for letter in word]
print(uppercase_letters)
Output:
['H', 'E', 'L', 'L', 'O']
Great job! You’ve learned how to use list comprehensions, the magic loops in Python. Keep practicing, and soon you’ll be a Python magician!