Featured Posts

Start Your Journey with Linux Command Line

Image
Start Your Journey with the Linux Command Line: A Comprehensive Guide Whether you are a software developer, system administrator, analytics engineer, or cybersecurity enthusiast, mastering the Linux command line (terminal) is one of the single most effective skills you can acquire. While graphical user interfaces (GUIs) offer visual convenience, the command line interface (CLI) delivers unmatched speed, fine-grained system control, and seamless automation capabilities. Linux Command Line This tutorial breaks down more than 35 core Linux commands into structured, practical modules complete with real-world examples, flags, and command-chaining techniques. By building muscle memory around these fundamentals, you will elevate your daily technical workflow from basic navigation to advanced command orchestration. Why Learn the Command Line? The Linux CLI is not merely a legacy tool—it remains the foundation of modern cloud architecture, server maintenance, DevOps pipelines, and enterp...

Python 3.6.1.9 LAB: Operating with lists - basics

Python 3.6.1.9 LAB: Operating with lists - basics

3.6.1.9 LAB
3.6.1.9 LAB

If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.6.1.9 LAB: Operating with lists - basics.

Objectives

Familiarize the student with:

  • list indexing;
  • utilizing the in and not in operators.

Scenario

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.

Solution Code

my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
new_list = [] # The helping list inside which we will add only unique numbers

for i in my_list:
    if i not in new_list: # The comparison part
        new_list.append(i) # Only ubique numbers will be added. Duplicates wont.
my_list = new_list # To update the original list. Or, you can print the new_list
                     instead.

print("The list with unique elements only:")
print(my_list)

And here's what is happening inside the code:

=================================================================================

Follow the Python page on the Blog to be the first to know

How to Remove Duplicates from a List

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.

Updating the Original List and Verifying the Result

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.

Key Takeaways

  • Create a separate empty list to hold the unique values rather than editing the list in place.
  • Use if i not in new_list to test whether a value has already been stored.
  • Call append() only when the membership test returns True so duplicates are skipped.
  • The final list retains the order in which each unique number was first encountered.
  • Reassigning my_list = new_list keeps the original variable name usable for later printing.
  • The skeleton list [1, 2, 4, 4, 1, 4, 2, 6, 2, 9] reduces to [1, 2, 4, 6, 9].

Frequently Asked Questions

Why should I use a helper list instead of deleting from the original?

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.

What do the 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.

Does this change the original list automatically?

No. You must explicitly reassign my_list = new_list if you want the original variable to hold the de-duplicated result.

Can I read the numbers from the user instead?

Yes. The skeleton hard-codes the list, but the same loop works if you collect input from the keyboard before processing it.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Open Source: The Invisible Engine of Your Daily Life

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst

Python 4.3.1.10 LAB: Converting fuel consumption

Python for Windows Beginners: Build Your First Automated Workflow in 10 Minutes