Getting started – Data Structures


Rishi Sapra

Technical Community leader, speaker, trainer and evangelist specialising in Power BI and Azure. Formally recognised by Microsoft as a Most Valuable Professional (MVP), Fast Track Recognised Solution Architect (FTRSA) and Microsoft Certified Trainer (MCT).


Getting started - Data Structures

In Python, data structures are a collection of data. More often than not, data stored in these structures are related to each other. In this section, four built-in data structures in Python will be discussed: list, tuple, dictionary, and set. They are used extensively in the Python How-to sections so make sure you go through the examples below.

The “list” data structure holds data as an ordered collection. In other words, there is a sequence. If you have worked with Excel, you will know about rows.

Imagine a row with several names. In Python, you separate the names with a comma. To make it a collection, Python then puts square brackets around it. To create a list of friends uses the following code:
friends = ['Jill, 'Ross, 'Mike', 'Jan']
‘Friends’ is now a variable as explained here. An important characteristic of lists is that lists are mutable. This means that we can make changes in lists themselves.

A tuple is a sequence of values much like a list. The important difference between list is that tuples are immutable. In other words, changes cannot be made. Just like lists, values stored in a tuple can be any type. Tuples do not have to be encapsulated by brackets. However, most codes do so and we would strongly recommend you do so too. The rest of the tutorials on this website shall consistently use brackets when dealing with tuples.
When you are more trained with programming you will automatically start sensing when to use tuples and list. Tuples will mostly be used when in a statement or user-defined function where the collection of values are not expected to change or not allowed to do so.
my_tuple = ("apple", "banana", "cherry")

A dictionary data structure is like a dictionary. Just like in a dictionary there will be a “word” and the “explanation”. Within Python the “word” is called key and “explanation” is called value. Important to remember is that the key must be unique i.e. a key can only appear only once in a dictionary.

my_dict = {
"brand": "Honda",
"model": "Accord",
"year": 2016

Keys of a dictionary can only be immutable objects while the values can be either immutable or mutable objects. The key-value pairs are separated by a colon and the pairs are separated themselves by commas and all this is enclosed in a pair of curly braces.

In Python, a set is a collection of unordered unique and immutable objects just like in maths. It can have any number of items and they may be of different types.  When we say objects are unique, we mean that sets, unlike lists or tuples, cannot the same object more than ones. You will see that with set you can test for membership. For example, you can test whether one set is a subset of another set.
my_set = {"apple", "banana", "cherry"}

Excercises: List

income = [1500, 2000,3500,1750, 4000]

# First we create a variable called 'car_prices' to store car prices
car_prices = [50000, 20000,15000,10000]
# Now we can make a total sum of car prices using the built-in sum() function
total = sum(car_prices)
print ('The total sum of car prices is:')
print (total)
# We can find out how many cars there are in the list by using the built-in len() function
number_of_cars = len(car_prices)
print ('The number of cars is:')
print (number_of_cars)
# We can make a variable called 'average_car_price' to calculate the average car price
average_car_price = total / number_of_cars
print ('The average price of a car is:')
print (average_car_price)
# We created a list called 'income'. Type in the shell 'income' and see the values in the list.
# Now insert in the list another income, 1000, by using the append fucntion income.append(1000)
# What do you get?
# creat a variable called min to see what the lowest income is. Use the min function: min().
# creat a variable called max to see what the highest income is. Use the max function: max().
# substract max from min to see the range in income.

Excercises: Tuple

income_tuple = (1500, 2000,3500,1750, 4000)

my_tuple = ("John", "Jill", "Jack")
print (my_tuple)
# we cannot change a tuple
# Just like we inserted another object in list, try to insert another object in tuple
# Add "Jo" to the tuple: my_tuple[3] = "Jo"
# When you press run, what is the error message you get?
# Just like with lists, we can make calcuations with tuples.
# print out a dataset we have prepared called "income_tuple".
# try to perform the same calculations like we did with list excercise.

Excercises: Dictionary

income_tuple = (1500, 2000,3500,1750, 4000)

# let's start with creating a dictionary
cars_dict = {"Toyota": "Corrola", "Honda": "Accord","BMW": "3-series"}
# now let's retrieve data from the dictionary. We want to know what the model is of 'BMW'

Excercises: Sets

# let's first make a set
set_of_cars = {'Honda', 'Mazda', 'Toyota', 'BMW'}
print (set_of_cars)
# let's add another element into our set
# now print this set again
print (set_of_cars)
# what happens if we want to add BMW, which already in the set?
print (set_of_cars)
# the set still has only four element and BMW does not appear twice
# try to add more elements with the add() method and/or remove element with the remove() method.

Leave a comment