This post is going to be the first post that I write on the topic of Python and will cover data structures and various implementations.

I will be discussing lists, dictionaries, sets and tuples as well as loops and data structures.

// These are notes taken from my lectures.

Lists

The key points of a list are that they are ordered, flexible (don’t need to be initialised with a size) and can contain different data types within the list i.e. [1, “two”, 3.0] meaning they are heterogeneous. Lists can be thought of as being much like ArrayList that you would find in Java.

new_list = list() # create new list using list() function
new_list = [] # create new list using []'s

# both of the above do the same thing (create an empty list).

You can initialise lists with content.

b_list = [5]       # creates a list with one int element
b_list = ['a']     # creates a new list with one string element
b_list = [1,2,'a'] # creates a new list with mixed int & string elements

You can also use a constructor with any iteratable i.e. a string or list of characters.

b_list = list("Big")
# creates the list ['B','i','g']

To add elements to a list we can use append() and insert() methods. Append adds the element to the end of the list.

b_list = ["Two"]
b_list.append("Three")
# creates the list ["Two","Three"]

Insert adds an element to a list in a certain index position. If the given index is below 0 i.e. -1 then the new element will be added to the start of the list i.e. at index 0. If the given index is greater than the length of the list then the element will be added at the end of the list.

b_list.insert(0,"One")
# takes the list from above and makes ["One","Two","Three"]

Although it is good practice to be precise, python is very forgiving when it comes to adding elements.

To remove elements from a list we can use either the pop() method or remove() method.

The pop() method removes an element at a given index position and if no index position is specified then it will remove the last element.

a_list = ["One","Two","Three"]
a_list.pop(1)
# ["One","Three"]

The remove() method removes a specified value from the list.

a_list.remove("One")
# ["Three"]

We can also combine lists (something that we wouldn’t be able to do in Java) simply in Python by either adding the lists.

a_list = ["One","Two","Three"]
b_list = ["Four"]
combined = a_list + b_list
# combined = ["One","Two","Three","Four"]

Or by using the extend() method which appends the list.

a_list.extend(b_list)
# a_list = ["One","Two","Three","Four"]

Dictionaries (dict)

A dictionary or dict is similar to a HashMap in Java in that it uses a key to reference a value. In a dict the keys need to be unique and we need to remember that the order is not guaranteed i.e. this data set is unordered and can change as we add or remove values. We would access the value of an element in a dictionary via the key.

empty_dict = dict()
empty_dict = {}
# both of the above create an empty dict

We can initialise a dict with content.

new_dict = {"Eggs":1,"Ham":4,"Toast":2}

To add an element to an already populated dict we can do the following.

new_dict["Spam"] = 5
new_dict[7] = "Integer as key" # this will work as well

If we want to retrieve an element from a dict we can call the variable name along with the key for the element.

print(new_dict["Spam"])
# prints 5 (which was initialised above)

If we try to retrieve a value for a key that doesn’t exist then an error will be thrown.

To update a value in a dict you would simply give the key a new value.

new_dict["Spam"] = 15 # updates the Spam value in the dict

To remove a value from a dict we can use the pop() method as seen in the lists section of this post. When “popping” a value you would feed the key into the method.

new_dict.pop("Spam") # removes the "Spam" entry from the dict

Sets

A set is much like a list only it is an unordered collection of unique elements and won’t allow for duplicate entries. The order of the elements will be subject to change as new elements are added and elements can be of any type i.e. int, float or string etc. A set can be thought of as being like the keys of a dict (in that they are not ordered and need to be unique).

There is only one way to create an empty set and that is by using the set() method. The following is how you would initialise an empty set and initialise a populated set.

new_set_1 = set() # creates an empty set
new_set_2 = {1,2,3,"4",5.0} # cannot be initialised as empty using {}'s

To add elements to a set we can use the add() method.

new_set = set()
new_set.add(1) # adds an int 1 to the set

To remove elements from a set we can use the remove() method.

new_set.remove(1) # removes the int 1 from the set

Set also has operations that we could use. Union() would join the two sets together (ignoring duplicates).

s1 = {1,2,3}
s2 = {3,4,5}
s3 = s1.union(s2)
# returns {1,2,3,4,5}

Intersection() would be to find all elements that are contained within both sets

s4 = s1.intersection(s2)
# returns {3}

Tuples

The last data structure we are going to discuss in this post is tuples which is an immutable (non updatable) data structure that is ordered (similar to lists). We can create an empty tuple, a tuple with data in it and also a tuple from a pre existing list.

t1 = ()                 # creates an empty tuple
t2 = (1,2)              # creates a tuple with 2 elements
t3 = tuple([1,2,3,4])   # creates a tuple from a list

The only time we can change an element in a tuple is when we have a mutable object stored within it.

t4 = tuple("foo",[1,2],True)  # creates {"foo",[1,2],True}
t4[2].append(3)               # returns {"foo",[1,2,3],True}

The main reason we use tuples is when we want to return more than one value from a function which we will discuss when we talk about loops.

With tuples we can use object destructuring to assign elements in the set to new variables.

t1 = (1,2,3)
a,b,c = t1
# a = 1
# b = 2
# c = 3

Operations

We can always query the length or size of a data set by calling the len() function. This works on sets, lists, dicts and tuples.

a_list = [1,2,3,4,5]
len(a_list) # returns 5 as there are 5 elements in the list

Loops

In Python we wouldn’t loop through a data set using the length as an upper limit and looking at each element using an itterated index variable (a for loop).

# THIS IS SOMETHING WE WOULDN'T DO
a_list = [1,2,3,4,5]
index = 0
while index < len(a_list)
  print(a_list[index])
  index++
# THIS IS SOMETHING WE WOULDN'T DO

We would rather iterate through each element in the collection using a for-each loop as all data structures support iteration using the _iter_() method.

a_list = [1,2,3,4,5]

for value in a_list :
    print(value)

Sometimes we would need to know the index of an element when iterating through a list in which case we would use enumerate() method which returns a number (i, value) tuples. This will work for lists, sets and tuples.

a_list = [1,2,3,4,5]

for i, value in enumerate(a_list) :
    print(value, "at index", i)

If we want to iterate through a dict then we could write a for-each loop using the key as the iterate element.

a_dict = {1:"One",2:"Two",3:"Three",4:"Four"}

for key in a_dict :
    print(a_dict[key])

Or we could iterate through the values using the values() method.

for value in a_dict.values() :
    print(value)

If we wanted access to the keys while iterating through the values then we could use the items() method which will return a tuples object containing the key/value pair (which can then be used in the loop).

for key, value in a_dict.items() :
    print(value, "with key", key)

Leave a Comment

Your email address will not be published. Required fields are marked *