2. Python collection data types
Learning outcomes
At the end of this lesson, you will be able to:
- Understand the difference between a primitive data type and a collection data type
- Understand that a string is a list and demonstrate how to access its characters
- Understand and use the four basic collection data types in python: list, tuple, set and dictionary.
- Differentiate between collection data types based on whether it is ordered, indexed, mutable and allows duplicates.
Lists
Multiline strings
A list in python is exactly what its name suggests, a list of things. Like a list of numbers, names, or even a mix of both. To create a list, we have to follow a simple syntax rule: enclose the things in the list between two square brackets, like those [ and ], and separate between the list elements using commas, ,. So for example, here is a list of numbers: a = [4,6,7,1,0], a list of strings: a = ["a","?","neptune is a planet"], a list of both: a = [3,0,"Where is my car?"].
Well, you can also create a list of lists in python! And you can nest as many lists as you want. Here is an example: a = [[1,2],[3,4],[5,6]]. This is a list of three elements, each element being itself a list of two elements.
Accessing and changing list elements
To access a list element, we apply this syntax rule: find out what the order of that element is, and then access it using the square brackets. The order of an element in a list is an integer. The order of the first element is always 0. Here is an example.
a = ["I","want","to","order","the",4,"dollars","mocha",True]
print(a[7])
Result:
mocha
The string 'I' is the first element of list a, and therefore its index is 0 and can be retrieved by typing a[0].
We can change the element in a list by just assigning it a new value.
a = ["I","want","to","order","the",4,"dollars","mocha"]
a[0] = "yes you can!"
print(a[0])
Result:
dollars
Here we changed the value of the sixth element, with index 5, to 4.5.
So, what we've learned about lists is that they are ordered, indexed and can be changed i.e. mutable. A list can have elements with repeated values, such as a = [4,4,5], where the value 4 is duplicated. That is, a list allows duplicate values.
Strings are lists!
We said a string is a list of characters. It actually literally is. For example, in a = "Tom", a[0] is the character T, and so on.
Negative indexing
So far, our indexing start from 0 which correspond to the first element. What about indices less that 0? That's negative indexing. It is indexing that starts from the end of the list. The index -1 refers to the last element, -2 refers to the second last element, and so on.
Index ranges
We no know how to retrieve a single value from a list. What if we want to retrieve a set of values? In this case we will be retrieving a list from the list. To do that, we need to specify that starting and ending indices of the list of values we want to get from the list.
To retrieve a list of elements from a list, we use the :. For example, to retrieve all elements starting from the second up to the 5th element inclusive from the list a = [1,2,3,4,5,6,7,8,9,10], we do the following:
a = [1,2,3,4,5,6,7,8,9,10]
print(a[2:10])
Result:
[3, 4, 5, 6, 7, 8, 9, 10]
So in the syntax a:b, a is the starting index, whereas b is the index after the ending index. In the example above, the index of the 5th element is 4, but we wrote 1:5 instead of 1:4.
We can also use : in the following other ways:
- : will get you the entire list e.g. some_list[:]
- a: will get you all elements starting from those with index a e.g. some_list[a:]
- :a will get you all elements starting from the beginning of the list up to the element with index a-1 e.g. some_list[:a]
List length: len()
The size of the list can be obtained using len().
a = [1,2,3]
print(len(a))
Checking if a value exists in a list
To find if some value belongs to a given list, we use the in keyword in python. For example, given the list a = [1,2,3], the boolean expression 2 in a will return True because that's a correct statement.
Adding elements to a list
There are two ways to add elements to a list:
- By using the + operator e.g. [1,2,3]+[4] gives [1,2,3,4].
- By using the append() function e.g. a = [1,2,3];a.append(4);print(a) gives [1,2,3,4].
- By using the insert() function if you want to add an item at a specified index e.g. a = [1,2,3];a.insert(1,4);print(a) gives [1, 4, 2, 3].
Adding a list to a list
Let's say you have two lists a=[1,2] and b=[3,4], and you wish to create the list [[1,2],[3,4]]. Let's try to use + and append() and see if they will give us what we are after.
a = ['f','g',['h','u'],[5,6,7,[True,False]]]
print(a[3][3][0])
Result:
True
a,b=[1,2],[3,4]
print(a+b)
a.append(b)
print(a)
Result:
[1, 2, 3, 4]
[1, 2, [3, 4]]
The + didn't do the job, and the append() added b as an element in a. What we need to do is to create a third list and add a and b to it, like that:
a=[1,2]
b=[3,4]
c=[a]+[b]
print(c)
c=[]
c.append(a)
c.append(b)
print(c)
Result:
[[1, 2], [3, 4]]
[[1, 2], [3, 4]]
List functions
- append() Adds an element at the end of the list
- clear() Removes all the elements from the list
- copy() Returns a copy of the list
- count() Returns the number of elements with the specified value
- extend() Add the elements of a list (or any iterable), to the end of the current list
- index() Returns the index of the first element with the specified value
- insert() Adds an element at the specified position
- pop() Removes the element at the specified position
- remove() Removes the first item with the specified value
- reverse() Reverses the order of the list
- sort() Sorts the list
Tuples
A tuple, like a list, is a collection of things, but the things are enclosed between ( and ). But there is even a more important difference: once you group things in a tuple, you cannot change them. That is, a tuple is immutable.
For example, let's create a tuple and attempt to change the value of one of the elements.
a = (3,4,5)
a[0] = 2
Run the above code. You will get the error message TypeError: 'tuple' object does not support item assignment. It means, as I said above: you can't assign tuple elements.
A tuple of lists: the idea of an object in python
What about a tuple whose elements are lists? Can we change the elements, or the elements in the elements (i.e. lists)?
a = ([1,2],[3,4])
a[0][0]=2
print(a)
a[0]=[2,2]
No errors here! So what happened?
Simple: when you change the list element here, you are not changing the list itself. You are free to edit the content of this list, but you cannot change the list. So writting a[0]=2 will give the TypeError like above.
Tuple functions
You can work on tuples using the + operation, just like lists. As for functions, only 2 functions are supported for tuples:
- count() Counts the number of elements in the tuple
- index() Searches for a value in the tuple, and then returns its index if found
Sets
We use sets if we want to collect items without caring for their order. Items in a set have no order, or even an index.
Since that the items in a set does not have an index, it cannot be accessed!
But you can add and remove items to a set.
Sets are enclosed by { and }. For example, a = {1,2} is a set of two numbers.
Set functions
There are many functions for sets, but here we will just focus on the following functions:
- add() Adds an element to the set
- clear() |Removes all the elements from the set
- copy() Returns a copy of the set
- difference() Returns a set containing the difference between two or more sets
- discard() |Remove the specified item
- intersection() Returns a set, that is the intersection of two other sets
- isdisjoint() Returns whether two sets have a intersection or not
- issubset() Returns whether another set contains this set or not
- issuperset() Returns whether this set contains another set or not
- pop() Removes an element from the set
- remove() Removes the specified element
- union() Return a set containing the union of sets
- update() Update the set with the union of this set and others
Dictionaries
We learned in lists and tuples that the elements are indexed. The index is an integer that starts from 0. A dictionary extends the indexing concept: a dictionary is a collection of indexed objects, where the indices themselves can be anything immutable: numbers, float, strings and tuples (and frozensets, but we won't discuss that one today).
The syntax for creating a dictionary is as follows: {key:value}, where key is the index, value is any data type. For example,
a = {'apple':3.5, 'pear': 3.5, 'banana':4}
print(a['apple'])
b = {'a':"lists",'b':"tuples",'c':"sets",'d':"dictionaries"}
print(b['c'])
Result:
3.5
sets
To access the dictionary element, we use the bracket notation, just like in lisst and tuples. But the value passed in the bracket is a key.
Dictionary functions
- clear() Removes all the elements from the dictionary
- get() Returns the value of the specified key
- keys() Returns a list containing the dictionary's keys
- pop() Removes the element with the specified key
- values() Returns a list of all the values in the dictionary
