Beginner to Advanced List Comprehension Practice Problems | by Terence Shin | Towards Data Science

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!
# 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 8Find all of the numbers from 1–1000 that have a 6 in themCount 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
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
q1_answer = [num for num in nums if num % 8 == 0]
q2_answer = [num for num in nums if "6" in str(num)]
q3_answer = len([char for char in string if char == " "])
q4_answer = "".join([char for char in string if char not in ["a","e","i","o","u"]])
words = string.split(" ")q5_answer = [word for word in words if len(word) < 5]
q6_answer = {word:len(word) for word in words}
q7_answer = [num for num in nums if True in [True for divisor in range(2,10) if num % divisor == 0]]
q8_answer = {num:max([divisor for divisor in range(1,10) if num % divisor == 0]) for num in nums}
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:
towardsdatascience.com
and another one!
towardsdatascience.com
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!