Append Boolean to List in Python (4 Examples)
This tutorial explains how to add booleans to a list in Python programming.
Table of contents:
Let’s get started!
Example Data
I’ll use the following data as a basis for this Python tutorial:
my_list = [1, 2, 3, 'hello'] # Creating a list print(my_list) # Printing my_list # [1, 2, 3, 'hello']
As seen, my_list
is a mixed list containing three integers and a string.
Example 1: Apppend Single Boolean to List
The most common way to append a boolean to a list is to use the append() method. See the implementation below.
my_list.append(True) # Appending single boolean value to list print(my_list) # Printing my_list # [1, 2, 3, 'hello', True]
As seen above, the boolean value, True, has been appended to the end of my_list
.
Example 2: Append Multiple Booleans to List using extend()
One might also be interested in appending multiple boolean values to a list. You can implement this easily using the extend() method as follows.
my_list.extend((True, False)) # Appending multiple boolean values print(my_list) # Printing my_list # [1, 2, 3, 'hello', True, False]
As seen above, two boolean values, True and False, are appended to my_list
using a single line of code.
Are you still interested in another alternative? Then see the next example!
Example 3: Append Multiple Booleans to List using append()
It is also possible to use the append() method, like in Example 1, to add multiple booleans to a list. To achieve this, we need to define a for loop to repeat the appending process for each boolean item.
for item in [True, False]: # Appending multiple boolean values via for loop my_list.append(item) print(my_list) # Printing my_list # [1, 2, 3, 'hello', True, False]
As desired, the same output has been obtained with Example 2.
Are you looking for something even more straightforward? Then the next example is what you are looking for!
Example 4: Append Multiple Booleans to List using Concatenation Operator
You can also simply use the concatenation operator +
to add multiple boolean items to a list. All you need to do is to create a list containing boolean elements to be included, then add it to the existing list via the +
symbol.
my_list + [True, False] # Appending multiple boolean values by concatenation # [1, 2, 3, 'hello', True, False]
See, it is very easy! However, it is worth noting that the concatenation operator works differently from the append() and extend() methods. The append() and extend() methods work in place, which means they modify the object directly. On the other hand, the object remains the same when the concatenation operator is used.
print(my_list) # Printing my_list # [1, 2, 3, 'hello']
As seen, my_list
does not contain the boolean elements after the concatenation. You should explicitly assign the operation to an object. Both methods are useful depending on the purpose. For further details, see the article discussing the pros and cons of in-place algorithms.
Video, Further Resources & Summary
Would you like to learn more about including booleans in a list? Then I recommend looking at the following video instruction on my YouTube channel. I demonstrate the topics of the present article in the video.
The YouTube video will be added soon.
In addition, you might read the other posts on this homepage. I have released several tutorials about topics such as data conversion and lists.
- Convert List from 1/0 Dummy Integer to Boolean in Python
- Convert List from Boolean to 1/0 Dummy Integer in Python
- Convert List from Boolean to Float in Python
- Convert List from Float to Boolean in Python
- Python Programming Tutorials
You have learned in this article how to append booleans to Phyton lists in the Python programming language. If you have further questions, tell me about it in the comments section.
This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview of her other tasks on Statistics Globe.
Statistics Globe Newsletter