In this tutorial, I’m going to cover several different types of collections in Python.
Before we get started, let’s define what a collection is. A collection is similar to a basket that you can add and remove items from. In some cases, they are the same types of items, and in others they are different. Basically, it’s a storage construct that allows you to collect things.
For example, you might have a car type. You create several instances of the car and want some way to group all of those cars together and access them easily. This is the perfect scenario for a collection.
The collection will survive in memory. You don’t need to build the collection or create any type of scaffolding. All of that is provided for free. Just create a collection instance and start adding your cars. When you’re ready, you can pull them out by name or by index (position within the collection).
Python offers several built-in types that fall under a vague category called collections. While there isn’t a formal type called
collection in Python, there are lists, mappings, and sets.
In this tutorial, we’re going cover the following types:
Lists in Python are a built-in type called a sequence. List are mutable and allow you to add items of the same type or different types, making them very versatile constructs. Python lists are similar to arrays in other languages.
Python lists do allow you to have non-unique elements.
The following is an example of creating a list and adding items to it:
alist = ["item1", "item2", 4]
Notice the list is heterogenous, containing string and numeric types.
To retrieve an item from a list, simply reference the item’s index. Python lists are zero indexed. If I want the last item, which is position 3, I need to use an index of 2:
alist > 4
The number 4 is returned. When referencing a list item, just subtract one from its position to get the correct index value.
Checking the length of a list can be done using the
len(alist)) > 3
To add more items to the list, use the
alist.append(False) len(alist) > 4
We’ve increased the list by one and added a different type — the boolean. The list doesn’t complain at all.
We can delete elements by calling
remove() doesn’t return a value. The list will be updated and now contains three items:
['item1', 4, False]
There are a couple of other ways to get items out of a list. We saw how to access an item using its index. If I access index 2, I’ll get item 3:
thevalue = alist print(thevalue) > False
The above code will supply us with a copy of the item. That item is still in the list. The overall list count isn’t affected.
However, if we use
pop(), we get the item, but it is also removed from the list:
thevalue = alist.pop(1) print(thevalue) > 4 print("after pop", alist) > ['item1', False]
Lists can also be sorted. If I have the following list of strings:
alpha = ["z", "b", "a", "c"]
you can sort it using the
sort() doesn’t return a value. However,
alpha is now sorted. You can see this by printing the list:
Elements can be reversed just as easily by calling
reverse() also doesn’t return a value, and will reverse the current list.
Continue reading %A Tour of Python Collections%