Append value to List if not already present using Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Append value to list if not already present using Python
  2. Append a value to a List if a condition is met in Python
  3. Append to List if not None in Python

# 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]

append value to list if not already present

The code for this article is available on GitHub

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.

# Append multiple values to a List if not present

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]

append multiple values to list if not present

The code for this article is available on GitHub

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.

# Using Set objects as an alternative to lits

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']

using set objects as alternative to lists

The code for this article is available on GitHub

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}

# Append a value to a List if a condition is met in Python

To append a value to a list if a condition is met:

  1. Use a for loop to iterate over a collection.
  2. Check if the condition is met for each element.
  3. If the condition is met, append the value to the list.
main.py
first_list = [] second_list = [-5, 7, 14, -2, 9, -29] for item in second_list: if item > 0: first_list.append(item) print(first_list) # ๐Ÿ‘‰๏ธ [7, 14, 9]

append value to list if a condition is met

The code for this article is available on GitHub

We used a for loop to iterate over a list.

On each iteration, we check if the current number is greater than 0.

If the condition is met, we use the list.append() method to append the item to another list.

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

main.py
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

The method returns None as it mutates the original list.

You can also use an elif and else statements if you have to.

main.py
first_list = [] second_list = [-5, 7, 14, -2, 9, -29] for item in second_list: if item > 0: first_list.append(item) elif item < 0: first_list.append(item * 2) else: first_list.append(1) print(first_list) # ๐Ÿ‘‰๏ธ [-10, 7, 14, -4, 9, -58]

The elif statement checks if the item is less than 0 and the else statement is run if the item is equal to 0.

If you need to get access to the index of the current iteration, use the enumerate() function.

main.py
first_list = [] second_list = [-5, 7, 14, -2, 9, -29] for index, item in enumerate(second_list): if item > 0: first_list.append(item * index) print(first_list) # ๐Ÿ‘‰๏ธ [7, 28, 36]

The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐Ÿ‘‰๏ธ 0 bobby, 1 hadz, 2 com

# Append to List if not None in Python

To append a value to a list only if not None:

  1. Check if the value is not None.
  2. If the condition is met, use the list.append() method to add the value to the list.
main.py
my_list = [] value = 'apple' # โœ… append to list only if not None if value is not None: my_list.append(value) print(my_list) # ๐Ÿ‘‰๏ธ ['apple']

append to list if not none

The code for this article is available on GitHub

We first check if the value is not None.

You should use the is operator when you need to check if a variable stores a None value.

When we use is, we check for the object's identity.

The pep 8 style guide mentions that comparison to singletons like None should always be done with is or is not, and never the equality operators.

Use the equality operators (equals == and not equals != ) when you need to check if a value is equal to another value, e.g. 'abc' == 'abc'.

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

main.py
my_list = [] value = 'apple' # โœ… Append to list only if not None if value is not None: my_list.append(value) print(my_list) # ๐Ÿ‘‰๏ธ ['apple']

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

The method returns None as it mutates the original list.

# Filtering out the None values from a List

If you have a list that contains None values and you need to filter them out, use a list comprehension.

main.py
my_list = ['apple', None, 'banana', None] new_list = [i for i in my_list if i is not None] print(new_list) # ๐Ÿ‘‰๏ธ ['apple', 'banana']

filtering out none values from list

The code for this article is available on GitHub
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is None and return the result.

The new list doesn't contain any None values.

The list comprehension doesn't mutate the original list, it returns a new list.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev