The challenge asks students to:
1. Divisible By Ten
Let’s start our code challenges with a function that counts how many numbers are divisible by ten from a list of numbers. This function will accept a list of numbers as an input parameter and use a loop to check each of the numbers in the list. Every time a number is divisible by 10, a counter will be incremented and the final count will be returned. These are the steps we need to complete this:
- Define the function to accept one input parameter called
- Initialize a counter to 0
- Loop through every number in
- Within the loop, if any of the numbers are divisible by 10, increment our counter
- Return the final counter value
I understand how to solve the problem but I got curious and want to know the best way to remove numbers from a list. My code is listed below:
for number in nums:
if number % 10 != 0:
print(divisible_by_ten([20, 25, 30, 35, 40]))
###This returns [20, 30, 35, 40]… 25 was the only list item that was removed
As you’ll notice, 35 was left in the list. It seems like the computer only took out the first number that met the condition instead of removing all the number that met the condition. Can someone give me potential solutions that remove all list items from the list that meet the condition:
if number % 10 != 0:
The answer to that question will not involve a for loop as that is set up on the existing list. We do not want to interfere with the process that is already in motion.
This question should not involve removing anything from the list since we are only counting elements, not mutating the list. For instance, we can use a read-only loop to just access the data items. We do not need to know the indices.
count = 0
for x in nums:
if x % 10: continue
count += 1
Removing items from a list is a goal unto itself, and it should not be part of the process of analyzing a list. Say we have a problem: ‘Remove all the items that are not divisible by 10.’
The trick here is to not operate on the list we are iterating. Instead we make a shallow copy of the list and iterate over that as our ‘controlled structure’.
ctrl = nums[:] # or ctrl = nums.copy()
That will give us two identical lists (shallow copy).
for x in ctrl:
if x % 10:
Notice that the list we are iterating over (our control list) is not being mutated. We are removing items from the
nums list, which when returned will be devoid of all numbers not divisible by 10.
After you consider the above, take a look back through the forums for examples of what happens when we mutate a list we are iterating. Run a few examples and make a note of the outcomes. Bottom line, don’t remove items from a list we are iterating. It never ends well.
Thank you! This was very helpful!!!
You’re welcome. As an added bonus here’s another way to use Python to meet our goal (it may still be coming up in the learning path, so take this as a sneak preview).
>>> [x for x in [1, 2, 3, 4, 5] if x % 2]
[1, 3, 5]
>>> len([1, 3, 5])
However useful it is to remove in place, it still takes internal processes to perform that operation. Sometimes a generative outcome is the optimal approach. Above we did just that. It’s called, ‘list comprehension’ and as we can see, the language certainly comprehended our expressive logic. What makes this an unusual object is that it needs to be explained unless one is extremely intuitive and can mesh out the logic on their own. Kudos. Wish it was that way for all of us.
What is the final object? A list of odd numbers. Now look at the syntax. Everything is delimited by square brackets,
list notation. Now place one’s self inside the container.
Think of this process as depositional. The logic dictates what will be deposited from the iterable it is given to operate upon. We iterate, evaluate against a test condition, and approve those values that meet with requirements to be deposited in the alluvium (returned list).
Ultimately what we have here is a filter. We are able with this device to separate items of a given characteristic from a field of items. Very powerful stuff with minimal logic, something to keep in mind moving forward. When it comes up in the lessons, you might have the upper hand in terms of appreciating what this concept is all about.