Skip to content
Laviyx Learn

Try

Python Lab · Experiment 16

Remove duplicates from a list using a function

PythonWeek 4JNTUH R22 · CS206ES

Aim

To write a Python function remove_duplicates that takes a list and returns a new list containing each element of the original only once, leaving the original list unchanged.

Algorithm

  1. Define the function remove_duplicates(items).
  2. Start with an empty list unique.
  3. For each element x of items, if x is not already in unique, append it.
  4. Return unique.
  5. Test the function on a list of numbers and a list of strings, and print the original list again to show it is unchanged.

Program

Python
def remove_duplicates(items):
    """Return a new list with each element of items only once."""
    unique = []
    for x in items:
        if x not in unique:
            unique.append(x)
    return unique


numbers = [3, 1, 3, 7, 1, 9, 7, 3]
print("Original list   :", numbers)
print("Without repeats :", remove_duplicates(numbers))
print("Original after  :", numbers)
print()

fruits = ["mango", "apple", "mango", "banana", "apple"]
print("Original list   :", fruits)
print("Without repeats :", remove_duplicates(fruits))

Output

Output
Original list   : [3, 1, 3, 7, 1, 9, 7, 3]
Without repeats : [3, 1, 7, 9]
Original after  : [3, 1, 3, 7, 1, 9, 7, 3]

Original list   : ['mango', 'apple', 'mango', 'banana', 'apple']
Without repeats : ['mango', 'apple', 'banana']

Recorded by running this exact program.

How it works

The hint says the result does not have to keep the original order, which allows the quick answer list(set(items)). A set stores each value once, but in no fixed order: for strings the order can even change from one run of the program to the next. The loop above keeps the order of first appearance, which is usually what people expect, and it gives the same output every time.

The x not in unique test scans the unique list, so for very long lists the set method is faster. In Python 3.7 and later, list(dict.fromkeys(items)) is a fast way that also keeps the order.

Common mistakes

  • Removing items from the list while looping over it with for x in items. The loop skips elements and the original list is changed. Build a new list instead.

  • Forgetting return unique. The function then returns None.

  • Expecting list(set(items)) to keep the original order. A set has no order, so the result can come out in any order.

  • Using unique = items as the starting point. That is the same list, not a new one, so any change affects the caller's list.

Viva questions

Try to answer each one aloud before you open it.

  • How does the function know an element is a repeat?

    Before adding x it checks x not in unique. If x is already in the new list, it was seen before, so it is skipped.

  • How can sets remove duplicates?

    A set keeps only one copy of each value, so list(set(items)) gives the unique values, though not in the original order.

  • Why does the function build a new list instead of changing the given one?

    The task asks for a new list, and changing a list passed in would also change the caller's list, which can cause surprises elsewhere in the program.

  • What does the in operator do on a list?

    It returns True if the value is equal to any element of the list. It checks the elements one by one.

Learn the concepts

All Python Programming Lab experiments →