Append value to list if not already present using Python

avatar

Borislav Hadzhiev

Last updated: Aug 17, 2022

banner

Photo from Unsplash

Append value to list if not already present using Python #

To append a value to a list if not already present:

  1. Use the not in operator to check if the value is not in the list.
  2. Use the list.append() method to append the value to the list if it's not already present.
  3. The list won't contain any duplicate values.
main.py
my_list = [1, 2, 3, 4, 5] value = 6 if value not in my_list: my_list.append(value) else: print('The specified value is already present in the list') print(my_list) # 👉️ [1, 2, 3, 4, 5, 6] # ------------------------------------- new_list = [] list_of_values = [1, 1, 2, 2, 3, 3, 4, 4] for item in list_of_values: if item not in new_list: new_list.append(item) print(new_list) # 👉️ [1, 2, 3, 4]

We used the not in operator to check if a value is not present in a list before using the list.append() method.

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, check if each value is present in a list and only append values that are not present.

main.py
new_list = [] list_of_values = [1, 1, 2, 2, 3, 3, 4, 4] for item in list_of_values: if item not in new_list: new_list.append(item) print(new_list) # 👉️ [1, 2, 3, 4]

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

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

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

The new_list variable doesn't store any duplicates.

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', 'a', 'b', 'b', 'c', 'c'] my_set = list(set(list_of_values)) print(my_set) # 👉️ ['b', 'c', 'a']

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(1) my_set.add(1) my_set.add(2) my_set.add(2) my_set.add(3) my_set.add(3) print(my_set) # 👉️ {1, 2, 3}
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.