Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using Alternatives to sum()

00:00 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.

00:13 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.

00:55 With 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.

01:12 Here, both functions return the same result. This is due to the impossibility of accurately representing both 0.1 and 0.2 in binary floating point.

01:29 Unlike sum(), however, fsum() can help you reduce floating-point error propagation when you add very large and very small numbers together.

01:57 This second example is surprising and totally defeats sum(). With sum(), you get 0.0 as a result. This is quite far away from the correct result of 20000.0, as you get with fsum().

02:14 If you’re looking for a handy tool for concatenating or chaining a series of iterables, then consider using chain() from itertools.

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.

02:40 When you call chain(), you get an iterator of the items from the input iterables. In this example, you access successive items from numbers using next().

02:54 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.

03:16 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.

03:28 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.

03:41 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.

04:01 Using .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.