Python append to list if not in list

7 years later, allow me to give a one-liner solution by building on a previous answer. You could do the followwing:

numbers = [1, 2, 3]

to Add [3, 4, 5] into numbers without repeating 3, do the following:

numbers = list(set(numbers + [3, 4, 5]))

This results in 4 and 5 being added to numbers as in [1, 2, 3, 4, 5]

Explanation:

Now let me explain what happens, starting from the inside of the set() instruction, we took numbers and added 3, 4, and 5 to it which makes numbers look like [1, 2, 3, 3, 4, 5]. Then, we took that ([1, 2, 3, 3, 4, 5]) and transformed it into a set which gets rid of duplicates, resulting in the following {1, 2, 3, 4, 5}. Now since we wanted a list and not a set, we used the function list() to make that set ({1, 2, 3, 4, 5}) into a list, resulting in [1, 2, 3, 4, 5] which we assigned to the variable numbers

This, I believe, will work for all types of data in a list, and objects as well if done correctly.

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.

Copied!

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.

Copied!

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.

Copied!

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.

Copied!

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}

How do you append only unique elements in a list Python?

Either of the following ways can be used to get unique values from a list in Python:.
Python set() method..
Using Python list. append() method along with a for loop..
Using Python numpy. unique() method..

How do you append to an empty list in Python?

Use list..
a_list = [].
a_list. append("a").
print(a_list).

How do you check if something is not in a list Python?

“not in” operator − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false.

Can I append a list to a list Python?

append() adds a list inside of a list. Lists are objects, and when you use . append() to add another list into a list, the new items will be added as a single object (item).