![]() |
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] Python Iterate Over a List of FloatsBelow 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 loopIn this example, a list of floating-point numbers is iterated using a simple For loop, printing each number in the list on separate lines.
Output Using for loop: 3.14 2.718 1.618 0.707 Time complexity: O(n) – where n is the number of elements in the list. 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.
Output Using 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. Iterate Float List in Python Using a While LoopIn 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.
Output Using while loop: 3.14 2.718 1.618 0.707 Time complexity: O(n) where n is the length of the list. Iterate Over a List of Floats Using list comprehensionIn this example, list comprehension is employed to iterate over the float_list, printing each element on separate lines directly within the comprehension.
Output Using list comprehension: 3.14 2.718 1.618 0.707 Iterate Float List Using the map() functionIn 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.
Output Using map() function: 3.14 2.718 1.618 0.707 Time complexity: O(n), where n is the length of the list. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |