Flat vs. collection types in Python

Tue Nguyen
2 min readApr 15, 2022

--

We can informally divide Python built-in data types into two groups: flat types and collection types. Roughly speaking,

  • A variable of a flat type is associated with an object that can contain only one single element.
  • A variable of a collection type is associated an object that can contain potentially multiple elements.

Within the scope of this series

Flat types include

  • None type
  • Truth values
  • Integers
  • Real numbers
  • Complex numbers

Collection types include

  • Lists
  • Tuples
  • Ranges
  • Strings
  • Dictionaries
  • Sets &frozen sets

Callable types such as function or class can be considered flat types. However, since they are so different from the ones mentioned above, I will not combine them in the “Datatypes” sections.

Remarks

  • This classification of data type into flat and collection is informal rather than standard. Other people might not agree with me.
  • I will not cover complex numbers because as a data analyst/data scientist, you rarely use them.

Examples

Example 1: flat type examples

# Int
x = 5
print(x)
5# Float
x = 10.5
print(x)
10.5

Example 2: collection type examples

# List
x = [1, 2, 3]
print(x)
[1, 2, 3]# Set
x = {1, 2, 3}
print(x)
{1, 2, 3}

Why do we need collection type?

As you can see from previous articles, flat types such as int or float only allow a single element. Thus, if we want to store the prices of, says, 100 products, we need 100 variables, from product_1 to product_100. This is an ugly solution, and it's also not optimal. What if we have 20 more products next week? We will need 20 more variables, and this might break some other programs or analyses that were designed to deal with the original 100 variables only.

We need some more flexible data structures that can handle a collection of elements under the same name, for example, a list or a dictionary.

We will learn about these data structures in detail in the next chapters. For now, just take a look at some simple examples to see the benefits that a list could bring (don’t try to understand the technical details at the moment).

First, we create a list with 5 elements to store the prices of 5 products.

products = [100, 200, 50, 120, 80]

To see all elements at once, just print out the list.

products[100, 200, 50, 120, 80]

To access a specific element, we use the variable name and the element’s position (Python starts counting from 0)

# First element
products[0]
100# Third element
products[2]
50

Suppose, due to inflation, we want to increase the of all products by 5%. It’s very simple.

[1.05*x for x in products][105.0, 210.0, 52.5, 126.0, 84.0]

Or we can filter the list to get products whose prices >= 100 only.

[x for x in products if x >= 100][100, 200, 120]

We can also add more elements into the list.

products.extend([25, 75])
print(products)
[100, 200, 50, 120, 80, 25, 75]

Or remove some element

# Remove element with value 75
products.remove(75)
print(products)
[100, 200, 50, 120, 80, 25]

--

--

Tue Nguyen

Former data scientist. MSc student in quantitative economics. Love sharing data science stuff.