Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Ways to format elements of given list

Python | Ways to format elements of given list

Given a List of float values, the task is to truncate all float values to 2-decimal digits. In Python, we can format elements of a given list using various methods. Let’s see the different methods to do the task. 

Format Elements of Given List using List Comprehension 

In Python, we can format elements of a given list using List comprehension. Here is an example of the code that we are using for list comprehension to format elements.

Python3




# Python code to truncate float
# values to 2-decimal digits.
 
# List initialization
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
# Using list comprehension
Output = ["%.2f" % elem for elem in Input]
 
# Printing output
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Time Complexity: O(n), where n is the length of the list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the output list 

Format Elements of Given List using Map()

In Python, we can format elements of a given list using Map() function. Here is an example of the code that we are using Map() to format elements.

Python3




# Python code to truncate float
# values to 2 decimal digits.
 
# List initialization
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
# Using map
Output = map(lambda n: "%.2f" % n, Input)
 
# Converting to list
Output = list(Output)
 
# Print output
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Format Elements of Given List using Format()

In Python, we can format elements of a given list using Format() function. Here is an example of the code that we are using format() to format elements.

Python3




# Python code to truncate float
# values to 2 decimal digits.
 
# List initialization
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
# Using format
Output = ['{:.2f}'.format(elem) for elem in Input]
 
# Print output
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Format Elements of Given List using Iteration 

In Python, we can format elements of a given list using Iteration. Here is an example of the code that we are using iteration to format elements.

Python3




# Python code to truncate float
# values to 2 decimal digits.
 
# List initialization
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
# Output list initialization
Output = []
 
# Iterating
for elem in Input:
    Output.append("%.2f" % elem)
 
# Printing output
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Format Elements of Given List using Reduce

  • This code imports the functools module and uses its reduce function to apply a lambda function to each element in the list Input.
  • The lambda function takes in 2 arguments: acc (short for accumulator) and x, and adds the string representation of x with 2 decimal points to acc.
  • The initial value of acc is an empty list.
  • The reduce function iteratively applies the lambda function to each element in Input, starting with the initial value of acc and the first element of Input, and then using the result of the previous iteration as the new value of acc for the next iteration.
  • The final result of the reduce function is the list of strings with 2 decimal points for each element in Input.

Python3




import functools
 
 
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
Output = functools.reduce(lambda acc, x: acc + ["%.2f" % x], Input, [])
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Format Elements of Given List using Math Functions

One approach to truncate all float values to 2 decimal digits in a given list can be achieved by using the round() function. We can iterate through each element in the input list and apply the round() function to truncate the decimal values to 2 digits. The implementation of this approach can be done using the following algorithm:

Algorithm:

  • Initialize an empty list to store the truncated values.
  • Iterate through each element in the input list.
  • Apply the round() function to truncate the decimal values to 2 digits.
  • Append the truncated value to the output list.
  • Print the output list.
     

Python3




# List initialization
Input = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
# Output list initialization
Output = []
 
# Iterate through each element in the input list
for elem in Input:
    # Truncate the decimal values to 2 digits using the round() function
    truncated_value = round(elem, 2)
    # Append the truncated value to the output list
    Output.append(str(truncated_value))
 
# Print output
print(Output)


Output

['100.77', '17.23', '60.99', '300.84']


Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(n), as we need to create a new list to store the truncated values.

In terms of time and space complexity, this approach is similar to the first four methods mentioned in the article.

Format Elements of Given List using Counter Method

This code snippet shows how to format elements of a given list to two decimal places using the collections.Counter() method in Python. The input list is a list of floating-point numbers, and the output list is a list of strings with two decimal places.

1.Import the Counter method from the collections module.
2.Create an input list of float numbers.
3.Use the Counter method to count the occurrence of each element in the input list.
4.Use list comprehension to format each element in the input list to two decimal places using f-strings.
5.Store the resulting list in a variable and print it.

Python3




from collections import Counter
 
input_list = [100.7689454, 17.232999, 60.98867, 300.83748789]
 
formatted_list = [f"{num:.2f}" for num in Counter(input_list)]
 
print(formatted_list) # Output: ['100.77', '17.23', '60.99', '300.84']


Output

['100.77', '17.23', '60.99', '300.84']



Time Complexity: The time complexity of this approach depends on the size of the input list and the time complexity of the Counter method, which is usually O(n). The time complexity of the list comprehension is also O(n). Therefore, the overall time complexity of this approach is O(n).

Space Complexity: This approach creates a new list to store the formatted strings, which has the same length as the input list. Therefore, the space complexity of this approach is also O(n).

RELATED ARTICLES

Most Popular

Recent Comments