Python (3/7) Compound Datatypes

This is a part of the series of posts on Python Crash Course. Here, we take a step further into the language structures, to understand the compound data types, that are central to all Python code.

Compound data types in Python are quite similar to collections in Java. We have lists, tuples, sets and dictionaries. Let's peep into each of them one by one.


Python provides for several compound data types - that can be used to process data in groups. A list is the simplest of these. A list can contain a collection of any type of data, including other lists. To define a list, the variables need to be placed sequentially in square brackets. The code below describes the definition of a list and how its elements can be accessed.

Note that unlike many other languages, Python does not insist on having a consistent data type among the elements of the list. We can have a number, string, boolean and any other data types in the same list.

We can access the elements of the list using indexes in the square brackets.

l = [1, 'Hello', "World", True, [2, 'Learn', "Python", False]]

print(l)          # [1, 'Hello', 'World', True, [2, 'Learn', 'Python', False]]
print(l[0])       # 1
print(l[4])       # [2, 'Learn', 'Python', False]
print(l[4][0])    # 2
print(l[4][3])    # False
print(l[-3])      # World

Few lines of code say a lot more than what could be said in words. We can have indexing, nested indexing as well as reverse indexing.

Python lists also support splicing, and provide several utility functions to add / remove / change data in the list. Check out the code below:

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

print(letters)            # ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(letters[:])         # ['a', 'b', 'c', 'd', 'e', 'f', 'g']

letters[2:5] = ['C', 'D', 'E']
print(letters)            # ['a', 'b', 'C', 'D', 'E', 'f', 'g']

letters[2:5] = []
print(letters)            # ['a', 'b', 'f', 'g']

letters[2:2] = ['c', 'd', 'e']
print(letters)            # ['a', 'b', 'c', 'd', 'e', 'f', 'g']

print(letters)            # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(len(letters))       # 8

print(letters)            # ['b', 'c', 'd', 'e', 'f', 'g', 'h']

print(letters)            # ['b', 'c', 'd', 'e', 'f', 'g']

del letters[2]
print(letters)            # ['b', 'c', 'e', 'f', 'g']

del letters[2:4]
print(letters)            # ['b', 'c', 'g']

print(letters)            # ['g', 'c', 'b']

Notice that the output of print(letters) is same as that of print(letters[:]).

But, there is a difference. The letters[:] is not the same object as letters. It is a copy of the original - a shallow copy.


Tuples are similar to lists but, have one marked difference. Tuples are immutable. They cannot be changed once they are defined. Naturally, tuples provide most of the methods that lists provide - except any method that would modify the list. You might ask, what is the advantage of forcing such a restriction? It is speed! Due to its immutability, a tuple can be implemented differently from lists - with more focus on speed of execution. As mentioned before, Python being an interpreted language has to lag in speed. But, optimizations like these take it far ahead of others. Tuples are much faster than Lists and are used where we know that the data in the list is not going to change - this is a common scenario.

One major syntactical difference between List and Tuple is that a tuple is enclosed in circular brackets '(. . .)', while List is enclosed in square brackets '[. . .]'. You can convert a list to tuple and tuple to list by typecasting. Python also allows you to define a tuple without any brackets - because it is the most natural sequence for Python. A tuple can be converted to a list and a list can be converted to a tuple. Check out the code below for more.

t = (1, 2, 3)
print(t)         # (1, 2, 3)

t = 1, 2, 3
print(t)         # (1, 2, 3)

t = (1, 2, 'String', (3, 4, "String 2"), [1, 2, 3])
print(t)         # (1, 2, 'String', (3, 4, 'String 2'), [1, 2, 3])

print(t[4])      # [1, 2, 3]

t[4].extend([2, 3, 4])
print(t)         # (1, 2, 'String', (3, 4, 'String 2'), [1, 2, 3, 2, 3, 4])

l = list(t)
print(l)         # [1, 2, 'String', (3, 4, 'String 2'), [1, 2, 3, 2, 3, 4]]

t = tuple(l)
print(t)         # (1, 2, 'String', (3, 4, 'String 2'), [1, 2, 3, 2, 3, 4])

Note that although the tuple is immutable, a list contained in the tuple can be modified - because the tuple just contains the reference to the list object. The reference should not change. The list itself may be modified.


Sets are similar to their counterparts in other languages. As the name suggests, they ensure a distinct set of elements. Any duplicates are ignored. Sets do not have any order of elements. They are defined by data enclosed in curly braces - '{. . .}'. A set can be typecast to and from lists or tuples. Sets define various methods for manipulation.

s = {1, "String", ('1', 'Tuple'), 1, 2}
print(s)        # {1, 'String', 2, ('1', 'Tuple')}

print(s)        # {1, 'String', 2, ('1', 'Tuple')}

print(s)        # {1, 'String', 3, 2, ('1', 'Tuple')}

print(s)        # {'String', 3, 2, ('1', 'Tuple')}

# remove throws an exception and discard just ignores any attempt to remove non existent element
print(s)        # {'String', 3, 2, ('1', 'Tuple')}

print(s)        # {3, 2, ('1', 'Tuple')}

print(s)        # set()

Sets are choosy about the elements that they allow. For example, you cannot have a list inside a set. The elements have to be immutable and "hashable".


Dictionaries are a special set of keys with a value associated with each key. You can work with a dictionary as below:

d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
print(d)           # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

print(d['key1'])   # value1

d['key7'] = 'value7'
print(d)           # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key7': 'value7'}

del d['key7']
print(d)           # {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

d['key1'] = 'New Value 1'
print(d)           # {'key1': 'New Value 1', 'key2': 'value2', 'key3': 'value3'}

The above code describes the most common functionalities of a dictionary.