Only add unique values to a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 17, 2022

banner

Photo from Unsplash

Only add unique values to a List in Python #

To only add unique values to a list:

  1. Use the not in operator to check if the value is not in the list.
  2. If the value is not in the list, use the list.append() method to add it.
  3. The list will only contain unique values.
main.py
my_list = ['a', 'b', 'c'] value = 'c' # ✅ Only add value to list if not already present if value not in my_list: my_list.append(value) else: # 👇️ this runs print('The specified value is already in the list') # ------------------------------------- new_list = [] list_of_values = ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'a'] for item in list_of_values: if item not in new_list: new_list.append(item) print(new_list) # 👉️ ['a', 'b', 'c', 'd', 'e', 'f']

We used the not in operator to only add unique values to a list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

If the value is not present in the list, we use the list.append() method to add it.

The list.append() method adds an item to the end of the list.

The method returns None as it mutates the original list.

You can use the same approach if you need to iterate over a collection of values and only add the ones that are not present in another list.

main.py
new_list = [] list_of_values = ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'a'] for item in list_of_values: if item not in new_list: new_list.append(item) print(new_list) # 👉️ ['a', 'b', 'c', 'd', 'e', 'f']

We used a for loop to iterate over the list of values.

On each iteration, we check if the current value is not present in the other list.

If the value is not present in the list, we use the list.append() method to add it.

The new_list variable only stores unique values.

An alternative approach to consider is to use a set object.

Set objects are an unordered collection of unique elements.
main.py
list_of_values = ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'a'] my_set = list(set(list_of_values)) # 👇️ ['b', 'd', 'a', 'c', 'f', 'e'] print(my_set)

We used the set() class to convert a list to a set object. Since set objects only store unique values, any duplicates get automatically removed.

However, you should only use this approach if the order of the elements is not important because set objects are unordered.

If you decide to use a set object, you don't have to check if the value is already present in the set.

You can directly use the set.add() method because duplicate values cannot be added to the set.

main.py
my_set = set() my_set.add('a') my_set.add('a') my_set.add('b') my_set.add('b') print(my_set) # 👉️ {'b', 'a'}

Calling the set.add() method multiple times with the same value has no effect.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.