Using Alternatives to sum()
Using Alternatives to
sum(). As you’ve already learned,
sum() is helpful for working with numeric values in general. However, when it comes to working with floating-point numbers, Python provides an alternative tool.
If your code is constantly summing floating-point numbers with
sum(), then you should consider using
math.fsum() instead. This function performs floating-point computations more carefully than
sum(), which improves the precision of your computation. According to its documentation,
fsum() avoids loss of precision by tracking multiple intermediate partial sums.
00:35 The documentation provides the example seen on-screen.
fsum(), you get a more precise result. However, you should note that
fsum() doesn’t solve the representation error in floating-point arithmetic. This example uncovers this limitation.
Here, both functions return the same result. This is due to the impossibility of accurately representing both
0.2 in binary floating point.
fsum() can help you reduce floating-point error propagation when you add very large and very small numbers together.
This second example is surprising and totally defeats
sum(), you get
0.0 as a result. This is quite far away from the correct result of
20000.0, as you get with
If you’re looking for a handy tool for concatenating or chaining a series of iterables, then consider using
02:21 This function can take multiple iterables and build an iterator that yields items from the first one, from the second one, and so on until it exhausts all the input iterables.
When you call
chain(), you get an iterator of the items from the input iterables. In this example, you access successive items from
If you want to work with a list instead, then you can use
list() to consume the iterator and return a regular Python list.
chain() is also a good option for flattening a list of lists in Python.
To flatten a list of lists with
chain(), you need to use the iterable unpacking operator (
*). This operator unpacks all the input variables so that
chain() can work with them and generate the corresponding iterator.
The final step is to call
list() to build the desired flat list. As you’ve already seen,
sum() doesn’t concatenate or join strings.
If you need to do so, then the preferred and fastest tool available in Python is the
str.join() method. This method takes a sequence of strings as an argument and returns a new, concatenated string.
.join() is the most efficient and Pythonic way to concatenate strings. Here, you use a list of strings as an argument and build a single string from the input. Note that
.join() uses the string on which you call the method as a separator during the concatenation. In this example, you call
.join() on a string that consists of a single space (
" ") so that the original strings from
greeting are separated by spaces in the final string.
04:30 In the next section of the course, you’ll take a look back at what you’ve learned.
Become a Member to join the conversation.