Last updated: Apr 9, 2024
Reading timeยท5 min
To append a value to a list if not already present:
not in
operator to check if the value is not in the list.list.append()
method to append the value to the list if it's not
already present.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]
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.
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 as an alternative to litsSet objects are an unordered collection of unique elements.
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.
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
.
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}
To append a value to a list if a condition is met:
for
loop to iterate over a collection.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]
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.
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.
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.
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.
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐๏ธ 0 bobby, 1 hadz, 2 com
To append a value to a list only if not None:
None
.list.append()
method to add the value to
the list.my_list = [] value = 'apple' # โ append to list only if not None if value is not None: my_list.append(value) print(my_list) # ๐๏ธ ['apple']
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.
==
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.
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.
If you have a list that contains None
values and you need to filter them out,
use a list comprehension.
my_list = ['apple', None, 'banana', None] new_list = [i for i in my_list if i is not None] print(new_list) # ๐๏ธ ['apple', 'banana']
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.
You can learn more about the related topics by checking out the following tutorials: