Compound Data types in Python

Compound Types

Compound data types are similar to collections in Java.

Lists

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.
l = [1, 'String 1', "String 2", True, \
         [2, 'String 3', "String 4", False]]

print(l)
print(l[0])
print(l[4])
print(l[4][0])
print(l[4][3])
print(l[-3])
Note the use of \ to escape the newline. That is required else a new line character is considered end of a line of code - which would cause a compilation error in this case. 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)
print(letters[:])

letters[2:5] = ['C', 'D', 'E']
print(letters)

letters[2:5] = []
print(letters)

letters[2:2] = ['c', 'd', 'e']
print(letters)

letters.append('h')
print(letters)
print(len(letters))

letters.remove('a')
print(letters)

letters.pop()
print(letters)

del letters[2]
print(letters)

del letters[2:4]
print(letters)

letters.reverse()
print(letters)
Notice that the output of print(letters) is same as that of print(letters[:]). There is a minor difference. The letters[:] is not the same object as letters. It is a copy of the original - a shallow copy.

Tuples

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)

t = 1, 2, 3
print(t)

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

print(t[4])
t[4].extend([2, 3, 4])

print(t)
l = list(t)

print(l)
t = tuple(l)

print(t)
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

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)

s.add(1)
print(s)

s.add(3)
print(s)

s.remove(1)
print(s)

s.discard("Strings")
print(s)

# remove throws an exception and discard just ignores any attempt to remove non existent element
s.pop()
print(s)

s.clear()
print(s)
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

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)
print(d['key1'])

d['key7'] = 'value7'
print(d)

del d['key7']
print(d)

d['key1'] = 'New Value 1'
print(d)
The above code describes the most common functionalities of a dictionary.