4.2 Building lists – literals, appending, and comprehensions
If we’ve decided to create a collection based on each item’s position in the container—a list—we have several ways of building this structure. We’ll look at a number of ways we can assemble a list object from the individual items.
In some cases, we’ll need a list because it allows duplicate values, unlike a set. This is common in statistical work. A different structure, called a multiset, can also be useful for a statistically oriented collection that permits duplicates. This collection is available in the standard library as collections.Counter.
4.2.1 Getting ready
Let’s say we need to do some statistical analyses of some file sizes. Here’s a short script that will provide us with the sizes of some files:
>>> from pathlib import Path
>>> home = Path.cwd() / "data" ...