Introduction
Data science begins with data, and how that data is stored determines how easily it can be cleaned, explored, and analyzed.
Python is a data science tool tat anyone who is interested in data science would be wise to utilize.
Python's four core data structures (lists, tuples, dictionaries, and sets) form the foundation for this work.
Lists hold ordered collections such as a column of measurements or a series of records. Tuples protect values that should not change, such as fixed coordinates or configuration settings. Dictionaries pair keys with values, which mirrors the labelled data found in JSON files, APIs, and tabular records. Sets store only unique values, which makes them well suited to removing duplicates and comparing datasets.
This guide covers how to create, access, modify, and remove data in each structure, with worked examples.
1. Lists
A list can hold data of any type, and data of various types can be stored in the same list.
Lists are mutable, i.e. the data in a list can be modified once the list has been created.
1.1 Creating a list
A list is defined using a set of square brackets.
list_name = []
1.2 Storing data in a list
Data in a list can be added during the creation of the list.
To illustrate this, let us create a list, mixed_bag with a mix of data types, and use it to explore how to add data to a list.
mixed_bag = [1, 2, 'bob']
Data can be added to an existing list using the following list functions:
1.2.1 .append() function
This function adds a data item to the end of the list.
mixed_bad.append(3)
print(mixed_bag)
# outputs [1, 2, 'bob', 3]
1.2.2 .insert() function
This function adds an item at a specified index in the list.
mixed_bag.insert(1, 'ross')
print(mixed_bag)
# outputs [1, 'ross', 2, 'bob']
1.2.3 .extend() function
This function adds multiple data items to the end of a list.
mixed_bag.extend([3, 'moon'])
print(mixed_bag)
# outputs [1, 2, 'bob', 3, 'moon']
1.3 Accessing data in a list
1.3.1 Indexing
Data in a list is accessed through indexing.
Every item in a list is assigned a value that identifies its position in the list; the first item is assigned the position 0.
We can access the item at position 1 in our list as follows:
print(mixed_bag[1])
# outputs 2
Python allows positive and negative indexing. Negative indexing allows one to select items from the end of the list, starting with the last item by calling the index -1.
we can access the last item in our list by negative indexing as follows:
print(mixed_bag[-1])
# outputs bob
1.3.2 Slicing
Slicing is a method that enables you to select multiple values in a list.
Syntax of slicing
list_name[start_index : stop_index : step]
start index indicates where the slicing will start.
stop index is endpoint exclusive, i.e. it is not included in the slice. Slicing stops at stop_index - 1.
Step is an optional parameter that indicates the increment value between each selected element. By default it is 1.
mixed_bag = [1, 2, 'bob', 3, 'ross', 5, 6]
print(mixed_bag[1:4])
# outputs [2, 'bob', 3]
# using step
print(mixed_bag[0:5:2])
# outputs [1, 'bob', 'ross']
1.4 Removing data from a list
To remove data from a list you can use the following functions:
1.4.1 .remove() function
It removes an item from a list based on the value of the item.
mixed_bag = [1, 2, 'bob', 3]
mixed_bag.remove(2)
print(mixed_bag)
# outputs [1, 'bob', 3]
1.4.2 .pop() function
This function removes an item from the list and returns the removed item. By default, it removes the last item in a list.
removed_item = mixed_bag.pop()
print(removed_item)
# outputs 3
You can refine the use of pop() by passing an index, to remove an item at a particular index and return it.
removed_item = mixed_bag.pop(-2)
print(removed_item)
# outputs bob
2. Tuples
A tuple is an immutable list.
The data in a tuple cannot be changed once the tuple is defined.
A tuple is defined using a set of parentheses.
tuple_name = ()
The data is placed in the parentheses, separated by commas.
Lets place the data in our prior list to a tuple.
mixed_tuple = (1, 2, 'bob', 3)
2.1 Accessing data in a tuple
Data in a tuple can be accessed using both positive and negative indexing.
mixed_tuple = (1, 2, 'bob', 3)
print(mixed_tuple[2])
# outputs bob
# using negative indexing
print(mixed_tuple[-1])
# outputs 3
You can also use slicing to access a section of a tuple.
print(mixed_tuple[:2])
# outputs (1, 2)
2.2 Modifying a tuple
Tuples are immutable, i.e. once a tuple is defined, attempting to modify a value in it, or adding a value to it, will raise an error.
mixed_tuple[2] = 5
# This error is thrown:
# TypeError: 'tuple' object does not support item assignment
2.3 Unpacking a tuple
You can unpack the items in a tuple into variables; this is called unpacking.
To do this, you must create variables equal in number to the items in the tuple you are unpacking.
mixed_tuple = (1, 2, 'bob', 3)
num_1, num_2, name, num_3 = mixed_tuple
print(f"num 1 : {num_1}") # outputs num 1 : 1
print(f"num 2 : {num_2}") # outputs num 2 : 2
print(f"num 3 : {num_3}") # outputs num 3 : 3
print(f"name : {name}") # outputs name : bob
3. Dictionaries
A Python dictionary stores data in a key and value pair format.
Each key in the dictionary must be unique, and the key must be an immutable type of data.
During dictionary definition, the key and value in each pair are separated by a colon (:), and multiple pairs in the dictionary are separated by commas. The list of pairs is surrounded by curly braces.
3.1 Syntax of a dictionary
dictionary_name = {
key_1 : value_1,
key_2 : value_2
}
3.2 Adding data to a dictionary
You can add new data to a dictionary by assigning a value to a new key that does not exist in the dictionary.
dict = {
1: 'bob',
2: 'ross',
}
dict['new'] = 'doe'
print(dict)
# outputs {1: 'bob', 2: 'ross', 'new': 'doe'}
You can modify data in the dictionary by assigning a new value to an existing key in the dictionary.
dict[1] = 'mon'
print(dict)
# outputs {1: 'mon', 2: 'ross'}
3.3 Accessing data in a dictionary
You access the value in a dictionary by calling its key. Remember that Python is case sensitive, thus one must call the key in the case it was stored.
If you call a non-existent key, an error occurs.
print(dict[1])
# outputs bob
3.4 Removing data from a dictionary
To remove data from a dictionary, you use the delete statement (del) or the pop() function.
When using del, you pass the dictionary name and the key of the value you want to remove. This will remove the key-value pair from the dictionary.
del dict[1]
print(dict)
# outputs {2: 'ross'}
Using the pop() function removes the key and its value and returns the value.
dict = {
1: 'bob',
2: 'ross',
}
removed = dict.pop(2)
print(removed) # outputs ross
print(dict) # {1: 'bob'}
You can remove all items in the dictionary using the clear() function. This removes all the key-value pairs and makes the dictionary empty.
dict = {
1: 'bob',
2: 'ross',
}
dict.clear()
print(dict) # outputs {}
3.5 Iteration over a dictionary
You can loop through a dictionary by its keys using the .keys() function.
dict = {
1: 'bob',
2: 'ross',
}
for key in dict:
print(key)
# outputs 1
# 2
You can also iterate over a dictionary by its values using the .values() function.
dict = {
1: 'bob',
2: 'ross',
}
for value in dict.values():
print(value)
# outputs bob
# ross
You can also use the .items() function to get both the key and value as a tuple while iterating over the dictionary.
dict = {
1: 'bob',
2: 'ross',
}
for key, value in dict.items():
print(f"{key}, {value}")
# outputs 1, bob
# 2, ross
4. Sets
A set is a collection of unique data; duplicate values are automatically removed.
Sets are unordered, such that data cannot be accessed using indexing, since the order of the data may change every time the set is called.
4.1 Defining a set
Sets are defined using curly brackets.
set = {1, 2, 'bob', 'ross', 2}
print(set) # 2 is only displayed once
4.2 Adding data to a set
You can add data to a set by using the .add() function.
set = {1, 2, 'bob', 'ross', 2}
set.add('water')
print(set)
# outputs {'ross', 1, 2, 'water', 'bob'}
4.3 Removing data from a set
4.3.1 .remove() function
You can remove an item from a set by using the .remove() function, where you pass the item to be removed.
If the item is not found in the set, an error occurs.
set = {1, 2, 'bob', 'ross', 2}
set.remove(2)
print(set)
# outputs {1, 'bob', 'ross'}
# Attempting to remove an item that does not exist
set.remove(5)
print(set)
# This error is shown: KeyError: 5
4.3.2 .discard() function
The .discard() function works like .remove() but differs in that if the item to be removed is not found in the set, no error occurs.
set = {1, 2, 'bob', 'ross', 2}
set.discard('bob')
print(set)
# outputs {1, 2, 'ross'}
# Attempting to remove an item that does not exist
set.discard(5)
print(set)
# outputs {1, 2, 'ross', 'bob'} i.e nothing has been removed
4.4 Set operations
Set operations use the characteristic of sets to store unique data to perform comparisons between data sets.
We can create two sets to explore set operations.
set_1 = {1, 2, 'bob', 'ross', 2}
set_2 = {1, 3, 5, 6, 'ross'}
4.4.1 Intersection (&)
This operation returns only the data available in both data sets.
in_both = set_1 & set_2
print(in_both)
# outputs {1, 'ross'}
4.4.2 Union (|)
This operation returns unique data that is available in either data set.
It simply retuns all the unique data items fron both data sets.
in_either = set_1 | set_2
print(in_either)
# outputs {1, 2, 3, 5, 6, 'ross', 'bob'}
4.4.3 Difference
This operation returns only the data that is in one data set but not in the other.
It returns the unique data in one data set when compared to another.
in_only_1 = set_1 - set_2
print(in_only_1)
# outputs {'bob', 2}
4.4.4 Symmetric difference
This operation returns the data that is in either data set but not in both.
not_replicated = set_1 ^ set_2
print(not_replicated)
# outputs {2, 3, 5, 6, 'bob'}
Conclusion
You have now seen how each structure stores data and which operations it supports. Lists and dictionaries are the workhorses for holding and reshaping data, tuples guarantee that certain values stay fixed, and sets make it quick to find duplicates and overlaps between datasets.
Understanding these basics will enable one to explore data science libraries such as pandas and NumPy that build on these ideas.
Top comments (1)
Long article, but is of great use. Understanding the Data structure and its use perfectly.