13. Introduction to collection types#

Author: Tue Nguyen

13.1. Outline#

  • Motivation for collection types

  • Hierarchy of collection types

13.2. Motivation#

As you can see from previous chapters, flat types such as int or float only allow a single element. What 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 such as 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

# Init a list with 5 elements to store prices of 5 products
products = [100, 200, 50, 120, 80]
# View all elements
products
[100, 200, 50, 120, 80]
# Get the first element
products[0]
100
# Get the third element
products[2]
50
# Get the last element
products[-1]
80
# Increase prices of all product by 5% 
# to account for the inflation
[1.05*x for x in products]
[105.0, 210.0, 52.5, 126.0, 84.0]
# Extract prices >= 100 only
[x for x in products if x >= 100]
[100, 200, 120]
# Add to more prices to the list
products.extend([25, 75])
print(products)
[100, 200, 50, 120, 80, 25, 75]

Or remove some element

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

13.3. Hierarchy of collection types#

Collection types can be divided further into 3 groups: sequences, mappings, and sets as shown below

13.3.1. Sequences#

  • A sequence is a collection of elements sharing the same name

  • Each element can be accessed using the sequence name and the element’s position (or index)

  • Python starts indexing at 0

  • Thus, if a sequence has N elements, then the elements are indexed by 0, 1, ..., N-1

  • Common built-in sequence types include

    • list

    • tuple

    • range

    • str

13.3.2. Mappings#

  • Currently, Python has only one data type in the this group

  • It is dict (or dictionary type)

  • A dict is a collection with each element being a pair of key-value

  • Example: user = {"name": "John", "age": 20}

  • There is no intrinsic order in a mapping

  • We access an element of a dictionary using the corresponding key. Ex: user["name"]

13.3.3. Sets#

  • A set in Python is pretty much the same as in mathematics

  • It is a collection of unique elements and has no intrinsic order

  • There is no way to access an element of a set

  • We can only check if a value is in a set or not

  • There is two types of sets in Python

    • set

    • frozenset