Data Structures
To analyze census data, we need more than just simple variables. We need Containers to hold collections of data.
Lists¶
Think of a list as a n array of items stored in sequential order. Lists are identified/created using square brackets [ ].
Lists are:
Ordered: the items have a defined order, and that order will not change
Mutable: they can be modified (change, add, remove items) after they are created
Allows duplicates: list items can have the same value
A list can contain objects of different data types (int, float, char, list). In fact, a list can contain other lists. This list within a list is known as a nested list.
# Creating a list of crops
crops = ["Palay", "Corn", "Coconut", "Mango"]Output
Accessing list items/elements¶
List items are accessed using their index. The first item on the list always has an index of 0.
# Getting first item in list
crops[0]Output
'Palay'A negative index (e.g. -1, -2) means to start from the end. -1 refers to the last item.
# Getting the 2nd to the last item in the list
crops[-2]Output
'Coconut'You can also get a range of values by specifying a range of indexes of where to start and where to end the range.
# Getting 2nd to 3rd item in list
crops[1:3]Output
['Corn', 'Coconut']Accessing items in nested lists¶
# get the 2nd item inside the 4th item in the list
crops[3][1]Output
'a'Modifying lists¶
Since lists are mutable, we can update the data.
# Changing an item (Correction)
crops[1] = "Yellow Corn"
print(crops)Output
['Palay', 'Yellow Corn', 'Coconut', 'Mango']
Removing items in a list
remove(): removes the first occurance of an itempop(): removes the specified index
crops = ["Palay", "Corn", "Coconut", "Mango"]
crops.remove("Mango") # Removes the first 'Mango' it finds
print(crops)Output
['Palay', 'Corn', 'Coconut']
crops.pop(-1)
print(crops)Output
['Palay', 'Corn']
Sort list items¶
sort(): sort list alphanumerically, by default
crops = ["Palay", "Corn", "Coconut", "Mango"]
crops.sort()
print(crops)Output
['Coconut', 'Corn', 'Mango', 'Palay']
NOTICE THAT THE LIST IS EDITED IN PLACE
Joining lists¶
+append(): appending all the items from one list to another using a for loopextend(): add elements from one list to another list
# +
crops1 = ["Palay", "Corn", "Coconut", "Mango"]
crops2 = ["Eggplant", "Ampalaya"]
crops3 = crops1 + crops2
print(crops3)Output
['Palay', 'Corn', 'Coconut', 'Mango', 'Eggplant', 'Ampalaya']
# append()
crops1 = ["Palay", "Corn", "Coconut", "Mango"]
crops2 = ["Eggplant", "Ampalaya"]
for x in crops2:
crops1.append(x)
print(crops1)Output
['Palay', 'Corn', 'Coconut', 'Mango', 'Eggplant', 'Ampalaya']
NOTICE THAT LIST1 IS EDITED
# extend()
crops1 = ["Palay", "Corn", "Coconut", "Mango"]
crops2 = ["Eggplant", "Ampalaya"]
crops1.extend(crops2)
print(crops1)Output
['Palay', 'Corn', 'Coconut', 'Mango', 'Eggplant', 'Ampalaya']
NOTICE THAT LIST1 IS EDITED
Looping lists and list comprehensions¶
# Using a for loop
crops_list = ["Palay", "Corn", "Coconut", "Mango"]
for crop in crops_list:
print(crop)Output
Palay
Corn
Coconut
Mango
# Using a for loop
crops_list = ["Palay", "Corn", "Coconut", "Mango"]
# List comprehension
[print(crop) for crop in crops_list]Output
Palay
Corn
Coconut
Mango
[None, None, None, None]CHALLENGE 01¶
How do you change a range of values for a list?
this_list = ['Maligaya Farm', 3.0, 'Palay', 'Mango']
# try to replace the 3.0 and 'Palay' with 3.34 and 'Corn' using one commandTuples¶
Tuples are immutable lists—meaning they cannot be modified once created. Items in a tuple can be accessed similar to that of lists. A tuple is identified/created using a parenthesis ( ).
Tuples are:
Ordered: the items have a defined order, and that order will not change
Immutable: they cannot be modified after they are created
Allows duplicates: tuple items can have the same value
You can convert between tuples in lists using the tuple and list functions.
Why use tuples instead of lists? They are more efficient than lists so it’s best to use them for data that we know won’t change.
# convert list to a tuple
crops = ["Palay", "Corn", "Coconut", "Mango"]
crops_tuple = tuple(crops)
crops_tupleOutput
('Palay', 'Corn', 'Coconut', 'Mango')Unpacking a tuple¶
# pack values
crops_tuple = ('Palay', 'Corn', 'Coconut', 'Mango')
# unpack values
(a, b, c, d) = crops_tuple
print(a)Output
Palay
Sets¶
Sets are:
Unordered: the items in a set do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
Immutable: they cannot be modified after they are created
Unique/Does not allow duplicates: sets cannot have two items with the same value
Sets are an easy way to get unique values from a list.
# Sample Python list with duplicates
sample_list = [1, 2, 3, 4, 2, 5, 6, 3, 7, 8, 9, 1, 5, 10, 11, 12, 6, 13, 14, 15, 7, 16, 17, 18, 9, 19, 20]
# Create a set from the list
unique_set = set(sample_list)
# Print the elements of the set
print("Original List:", sample_list)
print("Set without Duplicates:", unique_set)Output
Original List: [1, 2, 3, 4, 2, 5, 6, 3, 7, 8, 9, 1, 5, 10, 11, 12, 6, 13, 14, 15, 7, 16, 17, 18, 9, 19, 20]
Set without Duplicates: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
# Add new element to a set
unique_set.add(21)
unique_setOutput
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}# Adding an element that's already in the set will not change the set
unique_set.add(1)
unique_setOutput
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}Dictionaries (dicts)¶
Dictionaries are used to store data values in key:value pairs.
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
Dicts consist of a set of keys and values that provide the ability to perform and indexed lookup.
Dictionaries are identified/created using curly brackets { }.
Dicts are:
Mutable: they can be modified after they are created
Does not allow duplicate keys: dicts cannot have two items with the same key
# Create a dict
farm_record = {
"farm_id": "F-10203",
"operator": "Juan Dela Cruz",
"hectares": 2.5,
"crops": ["Palay", "Mongo"],
"irrigated": True
}Output
# get value of dict item with key 'grass'
farm_record['operator']Output
'Juan Dela Cruz'Dictionaries can also be created using the dict function.
farm_record = dict(farm_id='F-10203', operator='Juan Dela Cruz')
farm_recordOutput
{'farm_id': 'F-10203', 'operator': 'Juan Dela Cruz'}Accessing items¶
Dict items are accessed using their keys
# Key as index
op = farm_record["operator"]
print(op)Output
Juan Dela Cruz
# using the get function
op = farm_record.get("operator")
print(op)Output
Juan Dela Cruz
Get keys and values¶
keys(): gets keysvalues(): gets values
farm_record.keys()Output
dict_keys(['farm_id', 'operator'])farm_record.values()Output
dict_values(['F-10203', 'Juan Dela Cruz'])CHALLENGE 02¶
How would you iterate over the items in a dictionary?
You can check to see if a dict contains a certain key or value:
farm_record = dict(farm_id='F-10203', operator='Juan Dela Cruz')
'farm_id' in farm_recordOutput
True'farm_id' in farm_record.keys()True'farm_id' in farm_record.values()Output
FalseTry to access an non-existent key (e.g.‘palay’):
farm_record['palay']Output
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Cell In[31], line 1
----> 1 farm_record['palay']
KeyError: 'palay'You can check if a value exists before accessing it:
# if the key 'farm_id' is in the dict, print its value; if not, print a prompt
if 'farm_id' in farm_record:
print(farm_record['farm_id'])
else:
print("Key 'farm_id' not found.")Output
F-10203
You can also wrap the code block in a try/except bloc (more preferred and Pythonic way).
try:
print(farm_record['palay'])
except:
print("Key not found")Output
Key not found