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)
Share your thoughts in the comments
Please Login to comment...