Remove Duplicates from List Python Using in not in

When working with lists in Python, especially those generated from user input, file data, or API responses, it’s common to encounter duplicate values. One useful technique is to Remove Duplicates from List Python Using in not in, which is both simple and effective. These duplicates can lead to inaccurate results, redundancy, or bloated memory usage—especially in data analysis or automation tasks.

Removing duplicates is an essential step in data cleaning and ensuring unique results. While there are multiple ways to handle this in Python, this guide focuses on a simple, beginner-friendly approach using the in and not in operators. This method is not only easy to understand but also helps reinforce how Python checks for element membership in collections like lists.

What Are in and not in Operators in Python?

Python provides two intuitive membership operators: in and not in. These operators are used to check if an element exists in a sequence such as a list, tuple, or string.

Here’s a basic example:

3 in [1, 2, 3]      # True
4 not in [1, 2, 3]  # True

These expressions return boolean values (True or False) based on whether the item is present in the given list. They’re extremely useful for filtering data and making decisions while looping through sequences.

Why Use in/not in to Remove Duplicates?

Using in and not in to remove duplicates from a list has several beginner-friendly advantages:

  • Preserves Order: Unlike using sets, which are unordered, this method keeps the original order of elements.
  • Easy to Understand: It uses plain logic, making it ideal for those just starting with Python.
  • No Extra Imports Needed: No need for external libraries or functions—just core Python.
  • ✅ Great for Practice: It helps you build a foundational understanding of loops and conditional checks.

This approach is perfect for small to medium-sized datasets where performance is not a critical concern but readability and simplicity are.

Step-by-Step Code: Removing Duplicates

Let’s walk through a simple example of how to remove duplicate numbers from a list using the not in operator.

Python Code:

original_list = [1, 2, 2, 3, 4, 3, 5]
unique_list = []

for item in original_list:
    if item not in unique_list:
        unique_list.append(item)

print(unique_list)

Output:

[1, 2, 3, 4, 5]

Explanation:

  • original_list contains numbers with some duplicates.
  • unique_list is initialized as an empty list that will store only unique values.
  • The for loop goes through each item in original_list.
  • The if item not in unique_list: condition checks whether the item has already been added.
  • If it hasn’t, it’s appended to unique_list.
  • The final print statement shows the cleaned list with duplicates removed, while preserving the original order of elements.

Real-World Example

Let’s apply the same logic to a more relatable scenario—cleaning up survey data where users may select the same option multiple times.

Python Code:

preferences = ["Python", "Java", "Python", "C++", "Java"]
cleaned = []

for lang in preferences:
    if lang not in cleaned:
        cleaned.append(lang)

print(cleaned)

Output:

['Python', 'Java', 'C++']

Use Case Explanation:

In real-world applications like surveys, users may select their favorite programming languages multiple times due to form errors or manual input. This code ensures each language appears only once in the final list, maintaining clarity and avoiding redundant processing later on.

Using Remove Duplicates from List Python Using in not in is a simple and effective method, especially for beginners. It preserves element order and reinforces core programming logic.
👉 Explore more Python tutorials on Newtum to master data handling and coding techniques.

About The Author

Leave a Reply