Introduction to Data Structures and Algorithms With Python
Flavian Kyande
Posted on February 21, 2022
A data structure is a physical representation of how data is organized and manipulated. Data Structures are defined by how they can store and organize single data elements and the algorithms available for accessing and manipulating the data.
This article will cover the data structure part of Data Structures and Algorithms, also succeeding the previous Introduction to Python article.
Python 101: Introduction to Modern Python
Python has two types of data structures namely:
Built-In data structures, that are already defined within the Python language. Examples include lists, tuples, sets, dictionaries and
User-defined data structures give a user the power to define and control their functionality. Examples include stack, queues, trees, linked lists, graphs and hash maps.
We will look at some data structures used in Python, which are lists, tuples, sets, dictionaries, linked lists, stack and queues
Lists
Lists are built-in mutable(can be changed) data structures used to store multiple items in a single variable. Lists are created using the square brackets and each item in the list is enclosed in quotes and separated by a comma. They can also be created using the list() constructor.
# declaring a list with different data types
our-world = ['earth', 6371, True]
print(our-world)
# declaring a list with the constructor method, taking note of double parenthesis
our-world = list(('earth' 6371, True))
print(our-world)
Lists store multiple types of data and duplicates. Lists are also ordered thus making it easy for each item to be accessed using indexes, where the first item index is 0 and the last item index is n-1 where n is the number of items in the list.
Tuples
Tuples, just like lists, store multiple items in a variable. The only difference is that tuples are immutable(they can not be changed), and they are created by enclosing the items using parenthesis. Tuples can also be declared using the tuple() constructor.
# declaring a tuple with different data types
our-world = ('earth', 6371, True)
print(our-world)
# declaring a tuple with the constructor method, taking note of double parenthesis
our-world = tuple(('earth' 6371, True))
print(our-world)
Sets
Sets can be differentiated from tuples in the way that they do not have indexes hence are unindexed and unordered. They are also unchangeable once created but one can add or remove items from the set. Sets do not allow duplicate items, therefore, duplicate items in a set are ignored. Sets are declared by wrapping items in curly brackets or using the set() constructor.
# declaring a tuple with different data types
our-world = {'earth', 6371, True}
print(our-world)
# declaring a tuple with the constructor method, taking note of double parenthesis
our-world = set(('earth' 6371, True))
print(our-world)
Dictionaries
A dictionary in Python stores data in key: value pair format. The keys are used as a reference to the value paired with. Dictionaries are ordered, mutable and do not allow duplicate items keys. This is an example of a dictionary that stores each of the planet’s radius sizes in kilometres.
planet-dictionary = {
'Mercury': 2440,
'Venus': 6052,
'Earth': 6371,
'Mars': 3390,
'Jupiter': 69911,
'Saturn': 58232,
'Uranus': 25362,
'Neptune': 24622
}
Linked Lists
Imagine that you had a Python program containing a list of an unknown number of participants in a Kaggle competition that would update every time an entry would be received. The ordinary Python list would push all the other participants below the rank of a new entry by allocating new memory space and copying the values on their required positions. This would mean more delay in our program contrary to what we want.
Luckily, linked lists solve that issue. Linked lists contain several lists linked by nodes and pointers that have a reference value for each of the linked lists making it easy to introduce new data values. This method saves the program the hustle of allocating and copying data into new memory spaces.
Stack
Stack is a linear data structure that utilizes the Last In First Out(LIFO) sequence. A perfect example is when you open your browser history, the sites you visited are stacked in descending order such that the recently visited sites appear at the top of the history data.
In Python, a stack is achieved through the “.append()” and “.pop()” methods.
# creating an empty list
planets = []
planets.append('Jupiter')
planets.append('Earth')
planets.append('Venus')
# print the list
print(planets)
# remove the first item in the list
planets.pop()
# print the final list
print(planets)
The final output will be:
['Jupiter', 'Earth', 'Venus']
['Jupiter', 'Earth']
This signifies that Venus has been removed first, which was the last item we entered in the list.
Queues
Queues work opposite to stacks in that they process data in the First In First Out Sequence(FIFO). All the requests are handled in the order in which they were received.
In Python, queues are made by using the “.insert(0, item)” method, the code would be:
# creating an empty list
planets = []
# adding member items to the list
planets.insert(0, 'Jupiter')
planets.insert(0, 'Earth')
planets.insert(0, 'Venus')
# printing the list
print(planets)
The output will be:
['Venus', 'Earth', 'Jupiter']
When we want to remove the items using the “.pop()” method, the first item which was entered, Jupiter, will be the first item out of the list
Posted on February 21, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.