cups full of pens, pencils, paint brushes, and rulers

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 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:

Tuple1 = "hello", “how", “are", "you?"

Or you can create a tuple of Integers:

Tuple2=10,25,43,12,38

You can retrieve 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 counts 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:

Tuple1[1}

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

Tuple1="I","ate","some","Yummy","Pie"

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:

tuple1= "How old are you?", 33, "what is your zipcode?", 92106

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

Print(tuple1[0:2])

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:

mylist = []
print(list)

The output should just type <list>

Next, we can add some values:

mylist.append("apple")
mylist.append("banana")
mylist.append("orange")
mylist.append("pear")

Print

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

print(mylist)

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

mylist.index("orange")
output: 2

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 the orange from the list with the remove function:

mylist.remove("orange")

Sort

We can sort the list with the sort function:

mylist.sort()

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:

mylist.sort(reverse=True)

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:

mylist.reverse()

Count

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

mylist.count("apple")

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:

len(mylist)

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:

addresses = {
"Bob":"123 First St",
"Joe":"321 Second St",
"Sally":"213 3rd St" 
}

We can print the values in our dictionary like this:

print addresses

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

Add an address for Tom:

addresses["Tom"]="456 4th St"

Change Bob’s address:

addresses["Bob"]="654 4th St"

Lookup Joe’s address:

print addresses.get("Joe")

Delete Sally’s address:

del addresses["Sally"]

How many addresses are you in your address book?

print len(addresses)

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 also 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:

People_set = {"Bob","Sally","Joe","John"}

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

zombie_set = {"John","Gordon","Lestat"}

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:

population = People_set.union(zombie_set)

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

The output should be similar to this:

set(['Joe', 'Bob', 'John', 'Sally', 'Gordon', 'Lestat'])

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:

victims = People_set.intersection(zombie_set)

Running “print victims” should output:

set(['John'])

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

safe = People_set - zombie_set

Running “print safe” should output:

set(['Bob', 'Sally', 'Joe'])

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.