Append Float to List in Python (4 Examples)
In this tutorial, you’ll learn how to append foat numbers to a list in Python using several methods.
The article contains the following content blocks:
Let’s start right away!
Creating Example Data
The first step is to create a sample list that we can use later in the examples.
my_list = [1, 2, 3, 'hello'] # Creating a sample list print(my_list) # Printing my_list # [1, 2, 3, 'hello']
The previous output shows that
my_list consists of three integers and one string. Let’s add some floating-point numbers to this list!
Example 1: Apppend Single Float to List
In this example, I will show how to add a single float number to a list using the append() method.
my_list.append(3.5) # Appending a float number print(my_list) # Printing my_list # [1, 2, 3, 'hello', 3.5]
As shown, 3.5 has been successfully appended to the end of
my_list. Let’s see how the method changes while appending multiple floats!
Example 2: Append Multiple Floats to List using extend()
In Example 2, I will explain how to add multiple float values to a list via the extend() method.
my_list.extend((3.5, 4.55))# Appending multiple float numbers via extend print(my_list) # Printing my_list # [1, 2, 3, 'hello', 3.5, 4.55]
my_list has been extended with the new float elements, 3.5 and 4.55. Alternatively, we can use the append() method to append multiple floats to a list. Let’s check it in the next example!
Example 3: Append Multiple Floats to List using append()
In order to use the append() method for adding multiple float numbers to a list, we need to set a for loop to iterate the appending process for each float item.
for item in [3.5, 4.55]: # Appending multiple float numbers via for loop my_list.append(item) print(my_list) # Printing my_list # [1, 2, 3, 'hello', 3.5, 4.55]
As shown, 3.5 and 4.55 have been appended at each iteration, respectively. There is one more alternative that is easy to implement but less common. Let’s take a look!
Example 4: Append Multiple Floats to List using Concatenation Operator
This example explains how to use the
+ operator to insert float numbers into a list. All needs to be done is to create a list containing float elements to be included, then add it to the existing list via the
my_list + [3.5, 4.55] # Appending multiple float numbers by concatenation # [1, 2, 3, 'hello', 3.5, 4.55]
The float numbers are now added in
my_list. 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 original object remains the same when the concatenation operator is used.
print(my_list) # Printing my_list # [1, 2, 3, 'hello']
my_list still does not have the float elements. The operation should have been assigned to an object explicitly.
Video & Further Resources
Have a look at the following video on my YouTube channel. In the video, I’m explaining the examples of this article:
The YouTube video will be added soon.
In addition to the video, you may read some of the related tutorials on this homepage. A selection of articles is shown below:
- Convert List from Float to Boolean in Python in R
- Add Float to Each Element in List in Python
- Append to 2D List in Python
- Convert List from Character String to Float in Python
- Python Programming Tutorials
In this Python programming tutorial, you have learned how to insert floats to lists . Let me know if you have any additional comments or questions in the comments section. Furthermore, please subscribe to my email newsletter for regular updates on new tutorials.
This page was created in collaboration with Cansu Kebabci. 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