Intro to Collections in Python
This course is part of a series of content designed to help you learn to program with the Python programming language. Here we explore a category of object types referred to as collections.
- Describe the purpose of a collection
- List the four collection types which are integrated into the syntax
This course was designed for first-time developers wanting to learn Python. Existing developers may want to skip.
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: email@example.com. And one of our cloud experts will reply.
The Python runtime provides many different object types which model different concepts. Strings model sequences of characters - which makes them useful for text. Integers model whole numbers. Floats model numbers with decimal places. And booleans model the notion of True or False. In this lesson, we’re going to explore a category of object types referred to as collections.
Collections are object types which are used as containers for storing other objects. There are several different types of collections included in Python’s standard library. However, four of them in particular are so common that they’re integrated into the language syntax. These four collections are the:
- List type
- Set type
- Dictionary type
- Tuple type
These four object types are each used as a container to store other objects. Let’s review them. A list object can store other objects. Remember, everything in Python is an object. Strings, numbers, callables, other lists, and everything else. So we can store anything in a list. Here are some of the behaviors of lists. Lists can:
- Have objects added
- Have objects removed
- Be sorted
- Be reversed
- Be searched
Lists are commonly used to store related objects. For example:
- we could store multiple strings, each containing the names of a programming language
- we could store a list of scores which we could use to calculate the average score
From these two examples, you might get the impression that lists have to store objects of the same type. However, lists can actually store multiple object types in a single list. There are likely use cases where storing multiple object types in a list makes sense. Though, often a list will contain objects of the same type. Let’s review the set type. A set is conceptually similar to a list except that it can’t contain duplicate objects. Set objects will automatically remove duplicate objects. This is helpful for specific use cases where you require a collection of distinct objects. Where lists can store any object, sets have some limitations. For example, we can’t store a list inside a set. We won’t go into those limitations now. However, just know that not all objects can be stored in a set.
Here are some of the behaviors of sets. Sets:
- Contain only unique objects
- Can have objects added and removed
- Can be searched
- Can be compared to other sets
Using a set to remove duplicate objects in a list is a common use case. Another common use case is to compare sets. Sets kind of behave like a Venn diagram. We can use sets to determine if an object exists in one, both, or neither set. The next collection type we’ll review is the dictionary type. Dictionaries are used to store objects which we can retrieve using a key. The dictionary type is likely near the top of the list for most common Python object types. That’s because they’re so flexible. Dictionaries are a collection of key-value pairs. Where the keys and values are both objects which share some relation. Here are some of the behaviors of dictionaries. Dictionaries can:
- Have objects added which are bound to keys - There are some restrictions on which object types can be used as keys. However, there are no restrictions regarding which types can be used for values.
- Have objects removed
- Be searched
- Be merged with other dictionaries
The relation between the key and value in a dictionary has meaning. For example, a dictionary is well suited for tracking capital cities from around the world. The relationship between the key and value is that the key contains the country and the value contains the capital city. Dictionaries are extremely flexible and have many use cases. The final collection type we’ll review in this lesson is the tuple. A tuple is basically a limited type of list. Tuples are intended to store a fixed number of related objects. This example shows an example of storing two GPS coordinates.
It may be unclear at this point why tuples exist when lists perform a similar purpose. Tuples are used to store a fixed number of related objects. Where the number of objects stored is known to the developer when they’re writing the code. When using a list, sometimes you’ll know how many objects it’ll store. Though, often you won’t know because the list will be filled with objects which are pulled from something such as a database or file. In this example, our coordinates are represented by two string objects storing latitude and longitude. Individually these each represent part of our coordinates. Together they’re complete. That’s the intent of a tuple. It’s the combination of related objects.
These four object types are so commonly used that Python incorporated them into Python’s language syntax. Which means that each of these types can be created using a syntax specific to each type. We’ll cover the exact syntax in future content. However, for now, just know that these four collection types are used often enough to justify having their own additional syntax. Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
- Collections are used as containers to store multiple objects.
- Each type of collection serves a specific use case.
- Four common built-in collections are: Lists, Sets, Dictionaries, Tuples
Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.