Start Your Journey with Linux Command Line
| 3.6.1.9 LAB |
Familiarize the student with:
Imagine a list - not very long, not very complicated, just a simple list containing some integer numbers. Some of these numbers may be repeated, and this is the clue. We don't want any repetitions. We want them to be removed.
Your task is to write a program which removes all the number repetitions from the list. The goal is to have a list in which all the numbers appear not more than once.
Note: assume that the source list is hard-coded inside the code - you don't have to enter it from the keyboard. Of course, you can improve the code and add a part that can carry out a conversation with the user and obtain all the data from her/him.
Hint: we encourage you to create a new list as a temporary work area - you don't need to update the list in situ.
We've provided no test data, as that would be too easy. You can use our skeleton instead.
And here's what is happening inside the code:
=================================================================================
This lab asks you to take a simple list of integers, some of which are repeated, and produce a new list where every number appears no more than once. The recommended strategy is to build a temporary helper list instead of trying to modify the original list while you are iterating over it. This approach avoids the common mistake of changing the sequence that the loop is currently walking through, which can skip elements or cause confusing index errors.
The solution creates an empty list named new_list and then loops over every element i in my_list. For each value, the membership test if i not in new_list decides whether the number has been seen before. If it has not, it is appended with new_list.append(i); if it has already appeared, the condition fails and the duplicate is silently ignored. This is a clean application of the in and not in operators, which return True or False depending on whether a value exists somewhere in a sequence.
Once the loop finishes, new_list holds only the unique values in their original first-seen order. The sample then reassigns the original variable with my_list = new_list so that the rest of the code keeps working with the same name, though printing new_list directly would work just as well. This two-list pattern is a standard and readable way to filter a sequence, and it is easy to extend later by asking the user for input instead of relying on a hard-coded list. Checking the result with print() confirms that the original repeated values have been removed.
if i not in new_list to test whether a value has already been stored.append() only when the membership test returns True so duplicates are skipped.my_list = new_list keeps the original variable name usable for later printing.Removing items while iterating over the same list shifts indices and can cause elements to be skipped, so building a second list is safer and easier to understand.
in and not in operators do?They check membership in a sequence and return a boolean; not in is True only when the value is absent from the list.
No. You must explicitly reassign my_list = new_list if you want the original variable to hold the de-duplicated result.
Yes. The skeleton hard-codes the list, but the same loop works if you collect input from the keyboard before processing it.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.