Introduction
Suppose I wanted to create a list of numbers from 1 to 1000 and wrote the following code…
Can you figure out what's wrong with it?
numbers = []
for i in range(1,1001):
numbers.append(i)
Trick question. There's nothing wrong with the code above, BUT there's a better way to achieve the same result with a list comprehension:
numbers = [i for i in range(1, 1001)]
List comprehensions are great because they require less lines of code, are easier to comprehend, and are generally faster than a for loop.
While list comprehensions are not the most difficult concept to grasp, it can definitely seem unintuitive at first (it certainly was for me!).
That's why I'm presenting you with eight practice problems for you to drill this concept into your head! I'll start by providing you with the list of questions at the top and will subsequently provide the answers. Questions 1–5 will be relatively straightforward, while questions 6–8 will be a little more advanced.
With that said, here we go!
Questions
# Use for the questions below:
nums = [i for i in range(1,1001)]
string = "Practice Problems to Drill List Comprehension in Your Head."
- Find all of the numbers from 1–1000 that are divisible by 8
- Find all of the numbers from 1–1000 that have a 6 in them
- Count the number of spaces in a string (use string above)
- Remove all of the vowels in a string (use string above)
- Find all of the words in a string that are less than 5 letters (use string above)
- Use a dictionary comprehension to count the length of each word in a sentence (use string above)
- Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9)
- For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by
1. Find all of the numbers from 1–1000 that are divisible by 8
q1_answer = [num for num in nums if num % 8 == 0]
2. Find all of the numbers from 1–1000 that have a 6 in them
q2_answer = [num for num in nums if "6" in str(num)]
3. Count the number of spaces in a string
q3_answer = len([char for char in string if char == " "])
4. Remove all of the vowels in a string
q4_answer = "".join([char for char in string if char not in ["a","e","i","o","u"]])
5. Find all of the words in a string that are less than 5 letters
words = string.split(" ")
q5_answer = [word for word in words if len(word) < 5]
6. Use a dictionary comprehension to count the length of each word in a sentence
q6_answer = {word:len(word) for word in words}
7. Use a nested list comprehension to find all of the numbers from 1–1000 that are divisible by any single digit besides 1 (2–9)
q7_answer = [num for num in nums if True in [True for divisor in range(2,10) if num % divisor == 0]]
8. For all the numbers 1–1000, use a nested list/dictionary comprehension to find the highest single digit any of the numbers is divisible by
q8_answer = {num:max([divisor for divisor in range(1,10) if num % divisor == 0]) for num in nums}
Thanks for Reading!
I hope that you found this useful. If you were able to solve these questions with list/dictionary comprehensions, I think it's safe to say that you have a strong understanding of the concept.
If you found this useful and want more articles like this, please let me know in the comments :)
As always, I wish you the best in your learning endeavors.
Not sure what to read next? I've picked another article for you:
and another one!
Terence Shin
- If you enjoyed this, follow me on Medium for more
- Interested in collaborating? Let's connect on LinkedIn
- Sign up for my email list here!