Working with Python Collections

A collection in Python is an object that contains other objects. You can also think of it as a container or a bucket. There are many different kinds of collections contained within the containers module in Python. Today, we will discuss the different kinds of Python Collections/Containers. How to use the different kinds of Containers. and when you should use which one.

Data Types

The four main container types are:

  • List
  • Tuple
  • Dictionary
  • Set

All of these containers are similar to an array. But, they each have their own quirks.

Tuple

Tuples are static lists, meaning once they have been created, you cannot modify them. This is also called immutable. You can declare a Tuple by creating a comma separated list of items. For example, you can create a tuple of strings:

Or you can create a tuple of Integers:

You can retreive a value from within a tuple by specifying the index location. This is similar to an array The first element has an index of 0. Then it counds up from there. You can see the first value in our tuple of strings by running:

[code language=”plain”][/code]

If you want to see the second value, you would run:

If you want to change the value of Tuple1, you would run:

This might be confusing since I just said you could not change the value of a tuple. This is sort of a half truth. I am not changing the value of the tuple when I change Tuple1 to be equal to a new value. What I am doing is reassigning the variable to be pointed to a new object with a new value. Refer to our article on mutable vs immutable for a more detailed explanation.

Tuples don’t always have to have the same data types stored within them. As an example, you can store a combination of Integers and strings:

You can pull out a range of values at a time by using a slice value:

List

A list is similar to an array in other languages. Lists are mutable, meaning they can be modified after you create them. This is in contrast with tuples which are immutable, meaning they cannot be changed after you have created them.

Since lists are mutable, you have a good-sized list of functions for interacting with them. To name a few:

  • append
  • Remove
  • Sort
  • reverse
  • Count

Append

Append adds an additional element to the end of the list. We can demonstrate this by declaring an empty list:

The output should just be type <list>

Next we can add some values:

Print

Now when we print our list, we should see the list of elements in the array:

If we want to know the specific position of orange, we can use the index function:

We can show this is correct my looking up index location 2:

[code language=”plain”][/code]

Remove

The remove function is how you delete elements from your list. We can remove orange from the list with the remove function:

Sort

We can sort the list with the sort function:

Running the command above will sort your list from smallest to largest. Or alphabetical order. It depends on your dataset. Or, if you want to sort things in reverse order, you can add the reverse argument:

Reverse

You can use the reverse function to reverse the order of your list. You might be tempted to think of the reverse function as a shortcut to using the sort function with an argument. However,it does not quite work that way.

The reverse function simply reverses the order of a list. But does not change the order. whereas the sort function will sort everything largest to smallest. Or smallest to largest.

Here is how you can reverse the order of the list using the reverse function:

Count

We can see how many times apple appears in the list with the count function:

If we want to see how many elements in our array, there is not a function specific to lists, but we can use the len function:

Dictionary

A dictionary is a list of key/value pairs that you can query. An example of when you might use a dictionary is if you are building an address book. I ask for a person’s name, the dictionary returns the person’s address. Kind of a mini database.

Here is how we can declare a new dictionary called addresses with key/value pairs for people’s addresses:

 

We can print the values in our dictionary like this:

Here are some of the common functions you may want to use:

Add an address for Tom:

Change Bob’s address:

Lookup Joe’s address:

Delete Sally’s address:

How many addresses are you in your address book?

Set

A set is similar to a hashtable. It is a mutable unordered collection of unique values. It is highly optimized for checking to see if the collection contains a specific value. In addition to Sets there are alos Frozen Sets. Frozen Sets are the same thing. However, they are immutable.

One attribute about sets that you have to keep in mind is it does not keep items in any particular order. If you care about the order of your elements, use a different container type.

Sets have some very useful functions. And in the right use cases, can be a lot better than a traditional list.

Let’s assume you are in a City that is being taken over by Zombies. Let’s first create our set of people:

Next, let’s create a set with all of the Zombies in the city:

Now, if we want a complete list of everyone in the city, we can create a new set called population where we combine these two sets:

If we print out population, you can see the list of the city’s population:print population

The output should be similar to this:

Notice there are no duplicates in the output. Like I said earlier, Set’s consist of unique values.

People who are also in the zombie set could be considered victims. So let’s create a new set showing us who all the victims are:

Running “print victims” should output:

If we want a list of people who are not zombies, we could call that our set of safe people:

Running “print safe” should output:

Summary

We have covered the four primary types of containers in Python. There are many other types of containers. However, these are the four default containers that do not require any additional libraries.

%d bloggers like this: