Data Structure
Data structure is a collection of data values or objects that contain different data types. Here I’ll write about lists and tuples. Later I’ll explore dictionaries and sets as well.
List
A data structure that helps store and (if necessary) manipulate an ordered collection of items. Such as a list of email addresses.
Lists are similar to strings, that I covered in previous post.
Lists | Strings |
Data structure | Data type |
Allow duplicate elements | Allow duplicate elements |
Allow indexing and slicing | Allow indexing and slicing |
Sequences of elements | Sequences of characters |
Mutable | Immutable * |
*At first it might be hard to understand the term immutable. At the end of this section, I’ll provide more details.
Create a list
Both lines below, creates an empty list.
empty_list_1 = []
empty_list_2 = list()
Indexing and slicing
Omitting the starting index in a slice implies an index of zero, and omitting the ending index implies an index of len(my_list):
phrase = ['Astra', 'inclinant', 'sed', 'non', 'obligant']
print(phrase[:3])
print(phrase[3:]
['Astra', 'inclinant', 'sed']
['non', 'obligant']
List mutability
We can even change a slice of a list using the same logic. The slice can be of any length. The elements in the new list will be inserted in place of the indicated slice:
my_list = ['Macduff', 'Malcolm', 'Macbeth', 'Banquo']
my_list[1:3] = [1, 2, 3, 4]
print(my_list)
['Macduff', 1, 2, 3, 4, 'Banquo']
List Operations
Lists can be combined:
num_list = [1, 2, 3]
char_list = ['a', 'b', 'c']
num_list + char_list
[1, 2, 3, 'a', 'b', 'c']
They can also be multiplied, but cannot be subtracted or divided:
list_a = ['a', 'b', 'c']
list_a * 2
['a', 'b', 'c', 'a', 'b', 'c']
We can check whether a value is contained in a list by using the in operator:
num_list = [2, 4, 6]
print(5 in num_list)
print(5 not in num_list)
False
True
List methods
Lists are a core Python class and here are some useful methods to use with.
append()
my_list = [0, 1, 1, 2, 3]
variable = 5
my_list.append(variable)
print(my_list)
[0, 1, 1, 2, 3, 5]
insert()
my_list = ['a', 'b', 'd']
my_list.insert(2, 'c')
print(my_list)
['a', 'b', 'c', 'd']
remove()
my_list = ['a', 'b', 'd', 'a']
my_list.remove('a')
print(my_list)
['b', 'd', 'a']
pop()
Removes the item at the given position in the list, and returns it. If no index is specified, pop() removes and returns the last item in the list:
my_list = ['a', 'b', 'c']
print(my_list.pop())
print(my_list)
c
['a', 'b']
clear()
Removes all items:
my_list = ['a', 'b', 'c']
my_list.clear()
print(my_list)
[]
index()
Returns the index of the first occurrence of an item in the list:
my_list = ['a', 'b', 'c', 'a']
my_list.index('a')
0
count()
my_list = ['a', 'b', 'c', 'a']
my_list.count('a')
2
sort()
char_list = ['b', 'c', 'a']
num_list = [2, 3, 1]
char_list.sort()
num_list.sort(reverse=True)
print(char_list)
print(num_list)
['a', 'b', 'c']
[3, 2, 1]
More on immutable Strings
At first it was hard for me to understand the difference between the terms mutable and immutable. If I could modify the string, why doesn’t it count as mutable then? The answer lies in reassigning the value.
# Strings are immutable because we need to reassign them to modify them.
power = '1.21'
power = power + ' gigawatts'
print(power)
1.21 gigawatts
# We cannot reassign a specific character within a string.
power[0] = '2'
TypeError: 'str' object does not support item assignment
# Lists are mutable because we can overwrite their elements
power = [1.21, 'gigawatts']
power[0] = 2.21
print(power)
[2.21, 'gigawatts']
Tuple
Tuple is an immutable sequence that can contain elements of any data type.
As a data professional, sometimes it will be more important to access and reference data than to change and manipulate it. When we simply need to find some information, but keep the data intact, we can use a data structure called a tuple.
Tuples are kind of like lists, but they’re more secure because they cannot be changed as easily. They’re helpful because they keep data that needs to be processed together in the same structure.
Creating a tuple
First way to create a tuple is by using parentheses and/or commas.
# Tuples are instantiated with parentheses.
fullname = ('Masha', 'Z', 'Hopper')
# Tuples are immutable, so their elements cannot be overwritten.
fullname[2] = 'Copper'
print(fullname)
TypeError: 'tuple' object does not support item assignment
# We can combine tuples using addition.
fullname = fullname + ('Jr',)
print(fullname)
('Masha', 'Z', 'Hopper', 'Jr')
We can also create tuples with or without parentheses, but by using a trailing comma.
test1 = (1)
test2 = (2,)
print(type(test1))
print(type(test2))
<class 'int'>
<class 'tuple'>
tuple1 = 1,
tuple2 = 2, 3
print(type(tuple1))
print(type(tuple2))
<class 'tuple'>
<class 'tuple'>
Second way to create a tuple is by using the tuple() function.
# The tuple() function converts an object's data type to tuple.
fullname = ['Masha', 'Z', 'Hopper']
fullname = tuple(fullname)
print(fullname)
('Masha', 'Z', 'Hopper')
Functions and Unpacking a Tuple
# Functions that return multiple values return them in a tuple.
def to_dollars_cents(price):
'''
Split price (float) into dollars and cents.
'''
dollars = int(price // 1)
cents = round(price % 1 * 100)
return dollars, cents
to_dollars_cents(6.55)
(6, 55)
# "Unpacking" a tuple allows a tuple's elements to be assigned to variables.
dollars, cents = to_dollars_cents(6.55)
print(dollars + 1)
print(cents + 1)
7
56
# The data type of an element of an unpacked tuple is not necessarily a tuple.
type(dollars)
int
Storing data of different types
A big advantage of working with tuples is that they let us store data of different types inside other data structures. Here’s an example of how this might be useful.
# Create a list of tuples, each representing the name, age, and position
# of a player on a basketball team.
team = [('Marta', 20, 'center'),
('Ana', 22, 'point guard'),
('Gabi', 22, 'shooting guard'),
('Luz', 21, 'power forward'),
('Lorena', 19, 'small forward'),
]
The players themselves are individual records that are represented by tuples. They are a bit more secure because tuples are immutable and more difficult to accidentally change. Because lists and tuples are iterable, we can extract information from them using loops.
# Use a for loop to loop over the list, unpack the tuple at each iteration,
# and print one of the values.
for name, age, position in team:
print(name)
Marta
Ana
Gabi
Luz
Lorena
# This code produces the same result as the code in the cell above.
for player in team:
print(player[0])
Marta
Ana
Gabi
Luz
Lorena