How to Iterate Float List in Python
Last Updated :
18 Apr, 2024
We are given a list of floats and our task is to iterate the list of floats and print the result. In this article, we will see how to iterate the float list in Python.
Example:
Input: float_list = [3.14, 2.718, 1.618, 0.707]
Output:
Using for loop:
3.14
2.718
1.618
0.707
Explanation: Here, we are iterating over the list of floats and printing the result.
Python Iterate Over a List of Floats
Below are some of the ways by which we can iterate over a list of floats in Python:
Iterate Over a List of Floats Using the For loop
In this example, a list of floating-point numbers is iterated using a simple For loop, printing each number in the list on separate lines.
Python3
float_list = [3.14, 2.718, 1.618, 0.707]
# Iterate over the list using a for loop
print("Using for loop:")
for num in float_list:
print(num)
OutputUsing for loop:
3.14
2.718
1.618
0.707
Time complexity: O(n) – where n is the number of elements in the list.
Auxiliary space: O(1) – as we are not using any additional space.
Iterate Float List in Python Using For Loop and range()
In this example, a for loop with the range function is used to iterate over the indices of the float_list, accessing each element by index and printing them on separate lines.
Python3
float_list = [3.14, 2.718, 1.618, 0.707]
# Iterate over the list using for loop and range
print("Using for loop and range():")
for i in range(len(float_list)):
print(float_list[i])
OutputUsing for loop and range():
3.14
2.718
1.618
0.707
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), which is constant space
Iterate Float List in Python Using a While Loop
In this example, a while loop is utilized to iterate over the float_list by incrementing an index variable until it reaches the length of the list, printing each element on separate lines.
Python3
float_list = [3.14, 2.718, 1.618, 0.707]
# Iterate over the list using a while loop
print("Using while loop:")
i = 0
while i < len(float_list):
print(float_list[i])
i += 1
OutputUsing while loop:
3.14
2.718
1.618
0.707
Time complexity: O(n) where n is the length of the list.
Auxiliary space: O(1) as only a constant amount of extra space is used for variables i and length.
Iterate Over a List of Floats Using list comprehension
In this example, list comprehension is employed to iterate over the float_list, printing each element on separate lines directly within the comprehension.
Python3
float_list = [3.14, 2.718, 1.618, 0.707]
# Iterate over the list using list comprehension
print("Using list comprehension:")
[print(num) for num in float_list]
OutputUsing list comprehension:
3.14
2.718
1.618
0.707
Iterate Float List Using the map() function
In this example, the map() function is applied to iterate over the float_list, using the print function as the mapping function to print each element on separate lines. The result is wrapped in a list to display the output.
Python3
float_list = [3.14, 2.718, 1.618, 0.707]
# Iterate over the list using the map() function
print("Using map() function:")
list(map(print, float_list))
OutputUsing map() function:
3.14
2.718
1.618
0.707
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(1)
Similar Reads
How to iterate through a nested List in Python? A nested list is a list that contains other lists. Working with nested lists can seem tricky at first but it becomes easy once we understand how to iterate through them. This is the easiest way to loop through a nested list. We can use a for loop to access each sublist in the main list, and then use
3 min read
How to Add Floats to a List in Python Adding floats to a list in Python is simple and can be done in several ways. The easiest way to add a float to a list is by using the append() method. This method adds a single value to the end of the list.Pythona = [1.2, 3.4, 5.6] #Add a float value (7.8) to the end of the list a.append(7.8) print(
2 min read
How to Find Index of Item in Python List To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether weâre checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
2 min read
How to Modify a List While Iterating in Python Modifying a list while iterating can be tricky but there are several ways to do it safely. One simple way to modify a list while iterating is by using a for loop with the index of each item. This allows us to change specific items at certain positions.Pythona = [1, 2, 3, 4] for i in range(len(a)): i
2 min read
How to skip Iterations in For Loop - Python Loops are generally used to repeat the tasks. Sometimes, we may want to skip certain steps in the loop. We can do this easily with the continue statement. This article explains how to skip iterations in a for loop.In a for loop, you can use continue to skip a specific iteration when a condition is t
2 min read
How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth
2 min read